同志们,谁能帮帮我,实体框架5似乎没有 ApplyCurrentValues() 方法。是否有另一种方法来更新实体框架 v5 中的数据库对象。这是我想做的
odc.Accounts.Attach(new Account { AccountID = account.AccountID });
odc.Accounts.ApplyCurrentValues(account);
odc.SaveChanges();
但是我在ApplyCurrentValues()行中遇到了编译错误
同志们,谁能帮帮我,实体框架5似乎没有 ApplyCurrentValues() 方法。是否有另一种方法来更新实体框架 v5 中的数据库对象。这是我想做的
odc.Accounts.Attach(new Account { AccountID = account.AccountID });
odc.Accounts.ApplyCurrentValues(account);
odc.SaveChanges();
但是我在ApplyCurrentValues()行中遇到了编译错误
ApplyCurrentValues
是一种ObjectContext
API 方法,因此首先您必须获得对包含在以下内容中的 objectcontext 的访问权限DbContext
:
odc.Accounts.Attach(new Account { AccountID = account.AccountID });
((IObjectContextAdapter)odc).ObjectContext
.ApplyCurrentValues("Accounts", account);
odc.SaveChanges();
请注意,包装的上下文没有像“帐户”这样的成员,因此您必须使用该ObjectContext
方法本身。
但是您可以使用 DbContext API 执行相同的操作:
var sourceAcc = new Account { AccountID = account.AccountID });
odc.Entry(account).CurrentValues.SetValues(sourceAcc);