In EF, the update operation ID is auto-incrementing but not the primary key. Partial attributes of the object are updated based on ViewModel. efviewmodel
// ID auto-increment, but not the primary key
Public int Update_join <TEntity> (TEntity entity) where TEntity: class {dbcontext. set <TEntity> (). attach (entity); PropertyInfo [] props = entity. getType (). getProperties (); foreach (PropertyInfo prop in props) {if (prop. name = "ID") continue; if (prop. getValue (entity, null )! = Null) {if (prop. getValue (entity, null ). toString () = "& nbsp;") // some attributes are passed over due to foreground reasons. The value will be & nbsp; here we will handle dbcontext. entry (entity ). property (prop. name ). currentValue = null; dbcontext. entry (entity ). property (prop. name ). isModified = true;} return dbcontext. saveChanges ();}
Call method: db. Update_join (partySummaryTableEntity );
// Update partial attributes of an object based on ViewModel
Public int UpdateEntityFields <TEntity> (TEntity entity, List <string> fileds) where TEntity: class {if (entity! = Null & fileds! = Null) {dbcontext. set <TEntity> (). attach (entity); var SetEntry = (IObjectContextAdapter) dbcontext ). objectContext. objectStateManager. getObjectStateEntry (entity); foreach (var t in fileds) {SetEntry. setModifiedProperty (t) ;}} return dbcontext. saveChanges ();
}
// The preceding parameter list is a set of ViewModel attributes. The method is as follows:
Public static List <string> GetDeleteFiles () {List <string> list = new List <string> (); PropertyInfo [] props = new DeleteEntity (). getType (). getProperties (); // obtain the ViewModel Attribute Set foreach (PropertyInfo propertyInfo in props) {list. add (propertyInfo. name);} return list ;}
PS: eg A ViewModel:
Public class DeleteEntity: INDeleteAudited {public bool F_DeleteMark {get; set ;} /// <summary> /// the user who deletes the object /// </summary> public string F_DeleteUserId {get; set ;} /// <summary> /// object deletion time /// </summary> public DateTime? F_DeleteTime {get; set ;}}