更新EF6中的实体时出现主键异常

本文关键字:异常 EF6 实体 更新 | 更新日期: 2023-09-27 18:21:06

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

我需要能够根据发布对象上的一个属性更新第二个对象。下面的代码会产生错误。我在评论中留下了一些我试图让它发挥作用的片段。

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

更新EF6中的实体时出现主键异常

无论这是否是最佳实践,或者在技术上应该如何做到,这就是我解决问题的方法。

在进行任何其他操作之前,我传递的编辑对象需要首先标记为已修改。我假设这是因为上下文可以抓取它,并且所有其他与它相关的操作都将"在上下文中"进行。另一方面,我认为如果我试图将其附加到公司,它就是试图添加一个新对象。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);
    }

如果有人评论这是否是最佳实践,或者是否有更好的方法,我很乐意接受批评。