Goto: Several ways to update ASP. NET MVC + EF

Source: Internet
Author: User

1. Common

Db. Entry (entity). state = entitystate.modified;
Db. SaveChanges ();

2. Specify updates

Db. configuration.validateonsaveenabled = false;
Db. Tuser.attach (entity);
ObjectStateEntry stateentry = ((iobjectcontextadapter) db). ObjectContext.ObjectStateManager.GetObjectStateEntry (entity);
Stateentry.setmodifiedproperty ("Field 1");
Stateentry.setmodifiedproperty ("Field 2");
Db. SaveChanges ();
Db. Configuration.validateonsaveenabled = true;

3. Specify updates (Recommended!)

Tconsultant model = db. Tconsultant.firstordefault (t = = T.id = IID);
if (TryUpdateModel (model, new string[] {"Name", "Sex", "Phonenuber", "Certificatenumber", "Email", "QQ", "Culture"}))
{
Db. SaveChanges ();
Return Content ("modified successfully!");
}

4.

[HttpPost]/// <summary>          ///code to execute the modification/// </summary>          /// <param name= "model" ></param>          /// <returns></returns>           PublicActionResult Modify (blogarticle model) {Try              {                  //1. Add the Entity object A. To the EF object container, and B. Get the Pseudo wrapper class objectDbentityentry<blogarticle> entry = db. Entry<blogarticle>(model); //2. Set the state of the wrapper class object to unchangedEntry. State =System.Data.EntityState.Unchanged; //3. Setting the changed PropertiesEntry. Property (A = A.atitle). IsModified =true; Entry. Property (A= a.acontent). IsModified =true; Entry. Property (A= A.acate). IsModified =true; //4. Submit to the database to complete the modificationdb.                  SaveChanges (); //5. The update succeeds, then the command browser redirects to the/home/list method                returnRedirecttoaction ("Index","Home"); }              Catch(Exception ex) {returnContent ("modification Failed ~ ~ ~"+Ex.              Message); }} Upgrade version: Db. Entry (model). State=entitystate.unchanged;db. Entry (model). Property (M= = m. Field name). IsModified =true;d B.  SaveChanges (); 

Goto: Several ways to update ASP. NET MVC + EF

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.