什么相当于 ObjectContext.ApplyCurrentValues for EF



我正在使用这个

_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方法找到现有实体,该方法将在本地缓存中找到它或从数据库中检索它。在这两种情况下,您最终都会将实体实例附加到上下文。然后,使用传递的对象中的值更新该实例。

最新更新