【问题标题】:ObjectStateManager error对象状态管理器错误
【发布时间】:2013-05-08 04:11:20
【问题描述】:

我有控制器发布操作

public ActionResult Demographics(string submitButton, DemographicsViewModel model)
{
    switch (submitButton)
    {
        case "Home":
            return RedirectToAction("Index");
        case "Next Page":
            using (ProposalRepository proposalRepository = new ProposalRepository())
            {
                model.Proposal.UserID = proposalRepository.GetUserByName(MasterHelper.CurrentUsername).UserID;
                model.Proposal.CustomerID = proposalRepository.GetCustomerByName(model.CustomerName).CustomerID;

                if (model.Proposal.ProposalID != 0)
                {
                    proposalRepository.Update(model.Proposal);
                }
                else
                {
                    proposalRepository.AddProposal(model.Proposal);
                }

                proposalRepository.SaveChanges();
            }

            return RedirectToAction("GRQuestions", model.Proposal);
        default:
            return View();
    }
}

当我尝试更新时

public void Update(Proposal proposal)
{
    mContext.Proposals.ApplyCurrentValues(proposal);
}

抛出错误:在 ObjectStateManager 中找不到具有与所提供对象的键匹配的键的对象。验证所提供对象的键值是否与必须应用更改的对象的键值匹配。 我正在寻找解决方案,我发现了这个:

public ActionResult Demographics(string submitButton, DemographicsViewModel model)
{
    switch (submitButton)
    {
        case "Home":
            return RedirectToAction("Index");
        case "Next Page":
            using (ProposalRepository proposalRepository = new ProposalRepository())
            {
                Proposal proposal = proposalRepository.GetById(model.Proposal.ProposalID);

                model.Proposal.UserID = proposalRepository.GetUserByName(MasterHelper.CurrentUsername).UserID;
                model.Proposal.CustomerID = proposalRepository.GetCustomerByName(model.CustomerName).CustomerID;

                if (model.Proposal.ProposalID != 0)
                {
                    proposalRepository.Update(model.Proposal);
                }
                else
                {
                    proposalRepository.AddProposal(model.Proposal);
                }

                proposalRepository.SaveChanges();
            }

            return RedirectToAction("GRQuestions", model.Proposal);
        default:
            return View();
    }
}

只添加一行:

Proposal proposal = proposalRepository.GetById(model.Proposal.ProposalID);

效果很好,但我认为这是一个愚蠢的决定,我相信有更好的方法和解释???

【问题讨论】:

    标签: asp.net-mvc entity-framework-4


    【解决方案1】:

    ApplyCurrentValues 就是这样工作的。您必须先加载实体才能使用它(= 在修改附加实体时使用它)。如果您不想先加载它,请将您的Update 方法更改为:

    public void Update(Proposal proposal)
    {
        mContext.Proposals.Attach(proposal);
        mContext.ObjectStateManager.ChangeObjectState(proposal, EntityState.Modified);
    }
    

    【讨论】:

    • 抛出:发生了参照完整性约束冲突:定义参照约束的属性值在关系中的主体和依赖对象之间不一致。在 mContext.Proposals.Attach(proposal);
    • 您的Proposal 上是否有UserCustomer 的导航属性?他们的价值观是什么?
    • 你的意思是在 ModelView.Proposal 中吗?在页面上我有 m.Proposal.User.Username, new { @class= "customer_input", @readonly = "readonly" })%>
    • 这可能是问题所在。将 loded 用户和客户分配给导航属性。
    猜你喜欢
    • 1970-01-01
    • 2021-09-14
    • 2012-12-01
    • 2021-11-01
    • 2022-09-24
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多