EF的ObjectContext.ApplyCurrentValues相当于什么

时间:2017-07-07 16:20:54

标签: c# entity-framework

我正在使用此

 _obj.Entry(update).CurrentValues.SetValues(update);

但它无法正常更新

1 个答案:

答案 0 :(得分:2)

没有直接的等价物。最接近的是这样的:

var existing = context.Set<YourEntityType>().Find(update.Id); // pass your entity PK
if (existing == null)
    throw new InvalidOperationException(); // something is wrong
context.Entry(existing).CurrentValues.SetValues(update);

基本上,您使用Find方法找到现有实体,该方法将其定位在本地缓存中或从数据库中检索它。在这两种情况下,您最终都会将实体实例附加到上下文中。然后使用传递的对象中的值更新该实例。

相关问题