无法确定X关系的主要结束。多个添加的实体可以具有相同的主键

时间:2016-04-20 18:41:42

标签: c# entity-framework relationship

我知道还有其他人问过同样的问题,答案是处理引用而不是ID。

在我的情况下,我有一个奇怪的实体框架行为:它适用于一个案例(父子),但不适用于另一个案例(子孙)。

以下是我的模特:

public class Parent
{

    public int ID { get; set; }
    public string Name { get; set; }

    public List<Child> Children { get; set; } = new List<Child>();
}


public class Child
{

    public int ID { get; set; }
    public int ParentID { get; set; }       
    public string Name { get; set; }
    public List<GrandChild> GrandChildren { get; set; } = new List<GrandChild>();  
    public Parent Parent { get; set; }

}

public class GrandChild
{

    public int ID { get; set; }
    public int ChildID { get; set; }
    public String Name { get; set; }      
    public Child Child { get; set; }     

}

这是我的映射:

public class ParentConfig : EntityTypeConfiguration<Parent>
{
    public ParentConfig()
    {

        HasKey(e => e.ID);
        Property(e => e.ID).HasColumnName("ID");
        Property(e => e.Name).HasColumnName("Name");
        HasMany(e => e.Children).WithRequired(c => c.Parent).HasForeignKey(c => c.ParentID);

        ToTable("Parent");
    }
}

public class ChildMap : EntityTypeConfiguration<Child>
{
    public ChildMap()
    {

        HasKey(e => e.ID);

        Property(e => e.ID).HasColumnName("ID");
        Property(e => e.Name).HasColumnName("Name");
        Property(e => e.ParentID).HasColumnName("ParentID");

        HasMany(c => c.GrandChildren).WithRequired().HasForeignKey(c => c.ChildID);
        HasRequired(e => e.Parent).WithMany().HasForeignKey(e => e.ParentID);

        ToTable("Child");

    }
}


public class GrandChildMap : EntityTypeConfiguration<GrandChild>
{
    public GrandChildMap()
    {
        HasKey(e => e.ID);
        Property(e => e.ID).HasColumnName("ID");
        Property(e => e.ChildID).HasColumnName("ChildID");
        Property(e => e.Name).HasColumnName("Name");
        HasRequired(e => e.Child).WithMany().HasForeignKey(e => e.ChildID);
        ToTable("GrandChild");
    }
}

这是我的代码:

        Parent parent = new Parent { Name = "Parent", };
        Child child_1 = new Child { Name = "Child 1", Parent = parent };
        Child child_2 = new Child { Name = "Child 2", Parent = parent };
        GrandChild grandChild_1 = new GrandChild { Name = "GrandChild 1", Child = child_2 };
        GrandChild grandChild_2 = new GrandChild { Name = "GrandChild 2", Child = child_2 };

            context.Parents.Add(parent);

            //no need to call SaveChanges

            context.Children.Add(child_1);
            context.Children.Add(child_2);

            //SaveChanges() is needed here

            context.GrandChildren.Add(grandChild_1);
            context.GrandChildren.Add(grandChild_2);

            context.SaveChanges();

此代码失败并显示消息

  

&#39;无法确定Child_GrandChildren的主要结尾   关系。多个添加的实体可能具有相同的主键&#39;

但是如果我在添加孩子后保存,则有效,而我需要在添加父母后调用SaveChanges()

编辑:如果我删除属性List<GrandChild> GrandChildren它可以工作,但我确实需要它。

这是一个错误吗?

2 个答案:

答案 0 :(得分:2)

您需要将ChildMap中的关系配置更改为:

  HasMany(c => c.GrandChildren).WithRequired(gc=>gc.Child).HasForeignKey(c => c.ChildID);
  // the second one is not necessary, you already configure that relationship in ParentConfig
  //HasRequired(e => e.Parent).WithMany().HasForeignKey(e => e.ParentID);

答案 1 :(得分:1)

你需要做这样的事情:

parent.Children.Add(child1);
parent.Children.Add(child2);
child1.GrandChildren.Add(grandChild1);
....
context.SaveChanges();