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

拈花ヽ惹草 提交于 2019-12-06 04:23:09

问题


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();

But i have been getting compile error in the ApplyCurrentValues() line


回答1:


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();

Note that the wrapped context does not have members like "Accounts", so you have to use the ObjectContext method itself.

But you can do the same using the DbContext API:

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


来源:https://stackoverflow.com/questions/14579590/cant-get-the-applycurrentvaluesentity-to-work-in-entity-framework-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!