How to update a field in a table in Entity Framework

Source: Internet
Author: User

Statement: If you think the layout here is uncomfortable: We suggest you read here: how to update a field in the Table in Entity Framework

 

Generally, you may find that EF updates the values of a certain column in a database.

Basically, we use table and view to directly update the entire entity.

For example:

We update an object user (including the field ID, firstname, and lastname ).

 

Of course, before this update operation, we need to insert a piece of data into the table.

When this method is used for updating, the orm updates all the fields with ID 1 in the table. This is acceptable for tables with fewer fields, but if there are more than 50 fields in our table, the update operation will be ideal.

If we only need to update the firstname, can we simply update the firstname field? Yes. This requires the use of stored procedures.

You may have never tried the stored procedure in Entity Framework. In fact, it is similar to the use of table.

Create a stored procedure to update firstname.

When creating the ADO. NET data model in the next step, remember to add the stored procedure. After the model is created, (the edmx file is used to automatically generate the mapping code. Poco is not used ). Compared with using tables and views only, you need to select the stored procedure used by your table under the Mapping Details of the table to use the stored procedure as the datasource of the model. For example:

In this example, there is only one stored procedure. We only need to select updatefirstname. After compilation, you will see an additional function () in the edmx design mode ():

  1    public int UpdateFirstName(Nullable<global::System.Int32> iD, global::System.String firstName) 2         { 3             ObjectParameter iDParameter; 4             if (iD.HasValue) 5             { 6                 iDParameter = new ObjectParameter("ID", iD); 7             } 8             else 9             {10                 iDParameter = new ObjectParameter("ID", typeof(global::System.Int32));11             }12     13             ObjectParameter firstNameParameter;14             if (firstName != null)15             {16                 firstNameParameter = new ObjectParameter("FirstName", firstName);17             }18             else19             {20                 firstNameParameter = new ObjectParameter("FirstName", typeof(global::System.String));21             }22     23             return base.ExecuteFunction("UpdateFirstName", iDParameter, firstNameParameter);24         }25 

EF has automatically generated a function for the stored procedure and imported it into objectcontext. Amazing ..........

OK. Next we will use this update method. Simple:

 1 2             // Execute the Update First Name function 3             int i = EfDb.UpdateFirstName(1, "rename");4 5             // you will found the firstname has changed ,who Id is '1'6             EfDb.SaveChanges();

Query the result and you will find that the firstname field of the record with ID 1 has changed.

 

Cheers

Nic

Technorati tags: Entity Framework

 

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.