简体   繁体   English

违反主键限制-EF

[英]Violation of PRIMARY KEY restriction - EF

I have a problem trying to update a record in the DB. 我在尝试更新数据库中的记录时遇到问题。 I am using EF 6.2.0 in C # with .NET 4.7.2. 我在C#和.NET 4.7.2中使用EF 6.2.0。

public abstract class BaseEntity { }

public abstract class Entity<T> : BaseEntity, IEntity<T>
{
    //public virtual T Id { get; set; }
}

public class Order : Entity<long>
{
    public long OrderID { get; set; }

    ...
    ...
    ...

    public StatusEnum Status { get; set; }

    public virtual Option Options { get; set; } = new Option();

    public virtual ICollection<OrderDetail> Details { get; set; } = new List<OrderDetail>();
}

public class Option : Entity<long>
{
    public long OptionID { get; set; }

    ...
    ...
    ...

    public virtual Order Order { get; set; }
}

public class OrderDetail
{
    public long OrderDetailID { get; set; }

    ...
    ...
    ...

    public virtual Order Order { get; set; }
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    public DbSet<Order> Orders { get; set; }

    public DbSet<Option> Options { get; set; }

    ...
    ...
    ...

    modelBuilder.Entity<Order>()
        .HasOptional(x => x.Options)
        .WithRequired(x => x.Order);
}

using (var dbContext = new Context())
{
    var dbItem = dbContext.Orders.FirstOrDefault(...);
    if (dbItem != null)
    {
        dbItem.Status = StatusEnum.New;
        dbItem.Details.Add(orderDetail);
        dbContext.SaveChanges();
    }
}

When I try to update I get the following error 当我尝试更新时,出现以下错误

"Violation of PRIMARY KEY restriction 'PK_dbo.Option' Unable to insert a key duplicated in object 'dbo.Option'. The value of the duplicate key is (1). \ R \ nIf finished the instruction. "

I do not understand why try to add another "Option" record with the same ID 我不明白为什么要尝试添加另一个具有相同ID的“选项”记录

Something I saw is that at the time of obtaining the database record, Options comes with the correct data, but OrderID is 0, when it would have to be 1 (In the DB it has a 1) 我看到的是,在获取数据库记录时,Options附带了正确的数据,但是OrderID为0,当它必须为1时(在DB中为1)

What is the problem? 问题是什么? Thx! 谢谢!

You don't show that part of the code, but I assume that your OrderDetail entity has a dependant navigation property Order . 您没有显示代码的那部分,但是我假设您的OrderDetail实体具有相关的导航属性Order Now when EF begins to track OrderDetail after you added it, EF by default recursively also marks all its related entities (unless already tracked) as added. 现在,当添加后EF开始跟踪OrderDetail时,默认情况下,EF也会递归地将其所有相关实体(除非已被跟踪)标记为已添加。 In this case the state of Options are set to added via pricipal entity Order , so EF tries to insert them into database. 在这种情况下, Options的状态设置为通过主要实体Order添加,因此EF尝试将其插入数据库。
A solution to this would be either to: 解决方案是:
1) not set/unset the Order principal navigation property, but set only the foreign key OrderId ; 1)不设置/取消设置Order主体导航属性,而仅设置外键OrderId
2) call update on principal entity Order (in this case be sure that your Options are tracked in the proper state or navigation property is unset or else you might end up with the same exact problem). 2)调用主体实体Order更新(在这种情况下,请确保以正确的状态跟踪您的“ Options ,或者未设置导航属性,否则可能会遇到同样的问题)。

An excellent material on this: https://msdn.microsoft.com/en-us/magazine/dn166926.aspx 关于此的出色材料: https : //msdn.microsoft.com/en-us/magazine/dn166926.aspx

EDIT: apparently I'm blind because I missed the OrderDetail entity part. 编辑:显然我是盲目的,因为我错过了OrderDetail实体部分。 Well, at least I didn't miss my guess about the navigation property being there. 好吧,至少我没有错过关于导航属性的猜测。 :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM