entity framework 5 更新指定字段

dbSet.Attach(good);
var stateEntry = ((IObjectContextAdapter)context).ObjectContext.
ObjectStateManager.GetObjectStateEntry(good);
stateEntry.SetModified();
stateEntry.SetModifiedProperty("Title");
stateEntry.SetModifiedProperty("UpdateDate");
context.SaveChanges();

  如果出现

对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性

还需在SaveChanges前先关闭验证实体有效性(ValidateOnSaveEnabled)这个开关

最终代码
dbSet.Attach(good);
var stateEntry = ((IObjectContextAdapter)context).ObjectContext.
ObjectStateManager.GetObjectStateEntry(good);
stateEntry.SetModified();
stateEntry.SetModifiedProperty("Title");
stateEntry.SetModifiedProperty("UpdateDate");
context.Configuration.ValidateOnSaveEnabled = false;
context.SaveChanges();
context.Configuration.ValidateOnSaveEnabled = true;

  

上一篇:SqlHelper类


下一篇:Qt MVC(模型-视图-代理)