我正在用这个
_obj.Entry(update).CurrentValues.SetValues(update);
但不更新就不起作用
最佳答案
没有直接的等价物。最接近的是这样的:
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
方法来定位现有实体,该方法将在本地缓存中定位它或从数据库中检索它。在这两种情况下,您最终都会得到附加到上下文的实体实例。然后,您使用传递的对象中的值更新该实例。
关于c# - EF 的 ObjectContext.ApplyCurrentValues 等效项是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44975565/