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

回声

当我尝试在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);
    }
回声

无论是最佳实践还是从技术上讲,这都是我解决问题的方式。

在进行任何其他操作之前,我传入的已编辑对象必须先标记为已修改。我假设这是因为上下文随后可以获取它,并且与此有关的所有其他操作都将在“上下文内”完成。从其他方面来说,我认为如果尝试将新对象附加到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);
    }

如果有人对这是最佳做法还是有更好的方法发表意见,我很乐意提出批评。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

更新 EF6 中的实体子集合

来自分类Dev

在EF 6中更新现有数据会引发异常-“ ...相同类型的实体已经具有相同的主键值。”

来自分类Dev

EF6子实体未在多对多关系中更新

来自分类Dev

使用Linq返回EF6中的相关实体

来自分类Dev

阻止在EF6中为特定实体生成表

来自分类Dev

EF 6:移除实体会导致空对象

来自分类Dev

带有EF6运行时异常的实体框架'指定的成员映射无效'

来自分类Dev

WebApi的EF6合并实体

来自分类Dev

EF6实体的通用克隆

来自分类Dev

在 EF6 中加载相关实体

来自分类Dev

使用 EF 异常更新实体

来自分类Dev

这是在EF6中更新对象的好方法

来自分类Dev

这是在EF6中更新对象的好方法

来自分类Dev

在EF6中更新外键对象

来自分类Dev

为什么当 rowKey 属性是主键时 JPA 实体会抛出异常?

来自分类Dev

EF6中的多对多关系是否需要定义映射表并具有主键?

来自分类Dev

实体框架急于在EF4和EF6中加载差异

来自分类Dev

从EF4升级后,在EF6中查询数据的空引用异常

来自分类Dev

EF6表拆分与多个拆分的共享主键

来自分类Dev

添加复杂项目时违反EF6主键

来自分类Dev

EF6表拆分与多个拆分的共享主键

来自分类Dev

Firebird主键和EF6存储生成的模式

来自分类Dev

在ef中更新实体

来自分类Dev

在ef中更新实体

来自分类Dev

实体类型MVC5 EF6中的用户

来自分类Dev

如何在EF6异步中启动实体存储过程而不等待返回?

来自分类Dev

EF6忽略实体类中的[表]和[列]

来自分类Dev

在EF6中建模外键的复合键(建模为实体引用)

来自分类Dev

如何使用扩展实体在EF6类中通过属性更改通知创建新属性?