简体   繁体   English

无法使ApplyCurrentValues(Entity)在Entity Framework 5中工作

[英]Can't get the ApplyCurrentValues(Entity) to work in Entity Framework 5

Comrades, can anyone help me out here, entity framework 5 seems not to have ApplyCurrentValues() method. 同志们,任何人都可以帮助我,实体框架5似乎没有ApplyCurrentValues()方法。 Is there another way to update the database object in entity framework v5. 是否有另一种方法来更新实体框架v5中的数据库对象。 here is what am trying to do 这是我想要做的

odc.Accounts.Attach(new Account { AccountID = account.AccountID });
  odc.Accounts.ApplyCurrentValues(account);
  odc.SaveChanges();

But i have been getting compile error in the ApplyCurrentValues() line 但是我在ApplyCurrentValues()行中遇到了编译错误

ApplyCurrentValues is an ObjectContext API method, so first you have to gain access to the objectcontext that is wrapped in the DbContext : ApplyCurrentValues是一个ObjectContext API方法,因此首先您必须访问包含在DbContext中的DbContext

odc.Accounts.Attach(new Account { AccountID = account.AccountID });
((IObjectContextAdapter)odc).ObjectContext
    .ApplyCurrentValues("Accounts", account);
odc.SaveChanges();

Note that the wrapped context does not have members like "Accounts", so you have to use the ObjectContext method itself. 请注意,包装的上下文没有像“Accounts”这样的成员,因此您必须使用ObjectContext方法本身。

But you can do the same using the DbContext API: 但您可以使用DbContext API执行相同操作:

var sourceAcc = new Account { AccountID = account.AccountID });
odc.Entry(account).CurrentValues.SetValues(sourceAcc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM