开发者

updating an entity in entity framework

I have an entity (TerminalCertification) which has relation to other entities. I want to make user to be able to update TerminalCertification but I'm not able to update related object which is updated by user. My update code is here:

public void UpdateTe开发者_StackOverflow社区rminalCertification(TerminalCertification terminalCertification)
{
    var lastCertification = db.terminalCertifications.Find(terminalCertification.TerminalCertificationID);

    if (lastCertification==null)
        throw new TerminalCertificationNotFoundException(terminalCertification)                                   
        db.Entry(lastCertification).CurrentValues.SetValues(terminalCertification);
    }

I have searched stackoverflow and found below code but there is not such ObjectStateManager in DBContext class.

ObjectStateManager stateMgr = db.ObjectStateManager;    
ObjectStateEntry stateEntry = stateMgr.GetObjectStateEntry(model);
stateEntry.SetModified();

what should I do?


You can cast your DbContext to an IObjectContextAdapter which has the underlying ObjectContext and then use the ObjectStateManager off of that.


Use the UpdateModel or TryUpdateModel method inside your controller. If you are doing it outside of the controller then this will not work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜