更新在实体框架的实体 [英] updating an entity in entity framework

查看:116
本文介绍了更新在实体框架的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体( TerminalCertification ),其中有与其他实体。
我想使用户能够以更新 TerminalCertification ,但我无法更新这是由用户更新相关对象。我更新code是在这里:

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 UpdateTerminalCertification(TerminalCertification terminalCertification)
{
    var lastCertification = db.terminalCertifications.Find(terminalCertification.TerminalCertificationID);

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

我已经搜索计算器及以下​​code找到,但没有这样ObjectStateManager中的DbContext类。

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

我应该怎么办?

推荐答案

您可以投你的DbContext到IObjectContextAdapter具有潜在的ObjectContext,然后使用ObjectStateManager了这一点。

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

这篇关于更新在实体框架的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆