无法让 ApplyCurrentValues(Entity) 在 Entity Framework 5 中工作 [英] Can't get the ApplyCurrentValues(Entity) to work in Entity Framework 5
问题描述
同志们,谁能帮帮我,实体框架5似乎没有ApplyCurrentValues()方法.是否有另一种方法来更新实体框架 v5.5 中的数据库对象?这是我想要做的
Comrades, can anyone help me out here, entity framework 5 seems not to have ApplyCurrentValues() method. Is there another way to update the database object in entity framework v5. here is what am trying to do
odc.Accounts.Attach(new Account { AccountID = account.AccountID });
odc.Accounts.ApplyCurrentValues(account);
odc.SaveChanges();
但是我在 ApplyCurrentValues() 行中遇到编译错误
But i have been getting compile error in the ApplyCurrentValues() line
推荐答案
ApplyCurrentValues
是一个 ObjectContext
API 方法,所以首先你必须获得对 objectcontext 的访问权限包裹在 DbContext
中:
ApplyCurrentValues
is an ObjectContext
API method, so first you have to gain access to the objectcontext that is wrapped in the DbContext
:
odc.Accounts.Attach(new Account { AccountID = account.AccountID });
((IObjectContextAdapter)odc).ObjectContext
.ApplyCurrentValues("Accounts", account);
odc.SaveChanges();
请注意,包装的上下文没有像帐户"这样的成员,因此您必须使用 ObjectContext
方法本身.
Note that the wrapped context does not have members like "Accounts", so you have to use the ObjectContext
method itself.
但是您可以使用 DbContext API 执行相同的操作:
But you can do the same using the DbContext API:
var sourceAcc = new Account { AccountID = account.AccountID });
odc.Entry(account).CurrentValues.SetValues(sourceAcc);
这篇关于无法让 ApplyCurrentValues(Entity) 在 Entity Framework 5 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!