在EF6中更新实体会产生主键异常

时间:2015-11-08 02:18:31

标签: c# entity-framework asp.net-mvc-5

当我尝试在EF6中更新此对象时,我收到一条错误消息,指出有多个实体具有此主键。看看这个DB我知道这是不真实的(从我所看到的)。

我需要能够根据发布对象的其中一个属性更新第二个对象。下面的代码会产生错误。我已经注释掉了我试图让它发挥作用的作品。

public async Task<ActionResult> Edit(PricingRule pricingRule)
    {

        if(ModelState.IsValid)
        {
            var currentUser = await serv.UserManager.FindByIdAsync(User.Identity.GetUserId());
            var company = currentUser.Company;
            //var entityRule = serv.PricingService.PricingRules.Get(pricingRule.PricingRuleId);


            //If this is the first rule, set it to the company default
            var rulesCount = company.PricingRules.Count;
            if (rulesCount <= 1 || company.DefaultPricingRule == null)
                pricingRule.DefaultPricingRule = true;


            //Make sure no other rules are marked as default, and update the company with this rule as default
            if (pricingRule.DefaultPricingRule)
            {
                if (company.DefaultPricingRule != null)
                {
                    var oldRule = serv.PricingService.PricingRules.Get(company.DefaultPricingRule.PricingRuleId);
                    oldRule.DefaultPricingRule = false;
                    //serv.PricingService.PricingRules.Update(oldRule);
                }
                company.DefaultPricingRule = pricingRule;
                serv.CoreService.Companies.Update(company);

            }

            serv.PricingService.PricingRules.Update(pricingRule);
            await serv.SaveAllChangesAsync();
            return RedirectToAction("Index");
        }

        return View(pricingRule);
    }

1 个答案:

答案 0 :(得分:0)

无论是最佳做法还是技术应该如何做,这就是我解决问题的方法。

在进行任何其他操作之前,我传入的已编辑对象需要首先标记为已修改。我假设这是因为上下文可以抓住它,关于它的所有其他操作将在“上下文中”完成。另外我认为如果我试图将它附加到company.DefaultPricingRule,它会尝试添加一个新对象。

public async Task<ActionResult> Edit(PricingRule pricingRule)
    {

        if(ModelState.IsValid)
        {
            serv.PricingService.PricingRules.Update(pricingRule);
            var currentUser = await serv.UserManager.FindByIdAsync(User.Identity.GetUserId());
            var company = currentUser.Company;

            //If this is the first rule, set it to the company default
            var rulesCount = company.PricingRules.Count;
            if (rulesCount <= 1 || company.DefaultPricingRule == null)
                pricingRule.DefaultPricingRule = true;


            //Make sure no other rules are marked as default, and update the company with this rule as default
            if (pricingRule.DefaultPricingRule)
            {
                if (company.DefaultPricingRule != null)
                {
                    var oldRule = serv.PricingService.PricingRules.Get(company.DefaultPricingRule.PricingRuleId);
                    oldRule.DefaultPricingRule = false;
                    serv.PricingService.PricingRules.Update(oldRule);
                }

                company.DefaultPricingRule = pricingRule;
                serv.CoreService.Companies.Update(company);
            }

            await serv.SaveAllChangesAsync();
            return RedirectToAction("Index");
        }

        return View(pricingRule);
    }

如果有人评论这是最佳做法还是有更好的方法,我很乐意接受批评。