MVC + EF framework to make additions and deletions to databases

Source: Internet
Author: User

I've been looking at the MVC development model for the past few days, with the EF framework for adding and pruning databases. Now just a little summary.

When you use the EF to manipulate the database, ADO is added first. NET data model, a data context class is added for us, using objects of this class to do anything to the database. Therefore, you need to create a data context object before you can manipulate the database using EF.

Mydatabaseentities mde = new mydatabaseentities ();

1. use EF to add operations to the database

 1.1 Create an object that needs to be manipulated (in general, a table is an entity, that is, a class), with the Tblperson table as an example (int id,string name, string sex,nullable<int> Age) where age cannot be the null ID for the primary key, the autogrow type:

Tblperson personadd=new Tblperson ();

Personadd.name = "LSS";

personadd.sex= "Male";

personadd.age=23; ( supplemental Note: Since the Tblperson table specifies that age cannot be null, it must be assigned, which will be reflected in subsequent modifications to the delete operation )

Note here that because the ID is an auto-grow type, you do not need to assign a value to the ID when you add it.

1.2 Tell EF context to add operations to the database

Mde.tblperson. ADD (Personadd);

1.3 Saving changes to the entity to the database

MdE. SaveChanges ()

2. use EF to delete a database

2.1 Similarly create a Tblperson object

Tblperson persondelete =new Tblperson ();

When deleting and modifying the operation, be sure to indicate the primary key ID ( here is the ID, which is the primary key of a table, the table must specify the primary key when using EF to manipulate the table )

   //If we use EF to remove ID is 2 of the the record .

    persondelete.id=2;

Because age is non-empty, you need to assign an age value to whatever you want.

persondelete.age=23;

2.2.1 Method1 loading Persondelete objects into the EF entity container

Mde.tblPerson.Attach (Tblpersondelete);

2.2.2 tells the context that the delete operation will take place

Mde.tblPerson.Remove (tblpersondelete); 

2.2.3 Save the delete operation to the database

MdE. SaveChanges ();

Here the delete operation is done, the second method is described below

2.2.1 Method2 Loads the Persondelete object into the EF entity container and gets the returned entity object

Dbentityentry<tblperson> entrydelete = mde. Entry<tblperson> (Tblpersondelete);

2.2.2 Modifying the state of the entity object in the EF container to delete

entrydelete.state=system.data.entitystate.deleted

2.2.3 to save the deleted entity object

MdE. SaveChanges ();

3. use EF to make modifications to the database

3.1 Creating a Tblperson object

Tblperson tblpersonmodify=new Tblperson ();

You need to specify an ID when making a modification. Suppose we modify the data with ID 3.

tblpersonmodify.id=3;

Indicates the column to be modified if the name of the data with ID 3 is changed to Yzk

Tblpersonmodify.name= "Yzk";

At the same time, we need to assign an age value

tblpersonmodify.age=34;

3.2 Load the Tblpersonmodify object into the EF framework and get the returned entity object

Dnentityentry<tblperson> Entrymodify=mde. Entry<tblperson> (tblpersonmodify);

3.3 Modifying the storage state of an Tblpersonmodify entity object in the EF framework to unmodified

entrymodify.state=system.data.entitystate.unchanged;

3.4 Modify the attributes in the Tblpersonmodify object to true in the framework's entity state ismodified in the EF, indicating that this property requires EF to modify

Entrymodify.property (D=>d.name). Ismodified=true;

3.5 tells the context to save the modified entity object to the database

MdE. SaveChanges ();

4. querying the database using EF (SQO and LINQ)

4.1 Querying a database using Sqo

4.1.1 here produces a lazy load , through the database tools can know that this step does not go to the database query, but the SQL query language is saved

Dbquery<tblperson> dbquery= mde.tblPerson.Where (d=>d.name like "%l%") as dbquery<tblperson>;

4.1.2 When we use the ToList () method, we can only query from the database except for the data

List<tblperson> list=dbquery.tolist ();

4.2 Querying a database using LINQ statements

(from D in Mde.tblperson where D.name like "%l%" select D)

The above section of code return type is iqueryable<tblperson> generic collection

As with the above, you need to call the ToList () method to convert it to a list collection.

Here, using EF to query the database, modify, add, delete operation has been finished, I hope you will be a lot of advice together progress!!! ~~~~

MVC + EF framework to make additions and deletions to databases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.