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