简体   繁体   中英

Entity Framework code first multiple tier parent child foreign key

I am trying to implement a hierarchy using Entity Framework 6 and I am having trouble when I get past the first level of a parent child relationship. An example of what I am trying to do is below:

[Table("TestParent", Schema = "dbo")]
public class TestParent {
    [Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int TestParentId { get; set; }

    private ICollection<TestChild> _testChildren;

    public virtual ICollection<TestChild> TestChildren {
        get { return _testChildren ?? (_testChildren = new HashSet<TestChild>()); }
        set { _testChildren = value; }
    }
}

[Table("TestChild", Schema = "dbo")]
public class TestChild {
    [Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int TestId { get; set; }

    [Key, ForeignKey("TestParent"), Column(Order = 2)]
    public int TestParentId { get; set; }
    public virtual TestParent TestParent { get; set; }

    private ICollection<TestGrandChild> _testGrandChildren;

    public virtual ICollection<TestGrandChild> TestGrandChildren {
        get { return _testGrandChildren ?? (_testGrandChildren = new HashSet<TestGrandChild>()); }
        set { _testGrandChildren = value; }
    }
}

[Table("TestGrandChild", Schema = "dbo")]
public class TestGrandChild {
    [Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int TestGrandChildId { get; set; }

    [Key, Column(Order = 2)]
    public int TestChildId { get; set; }
    public virtual TestChild Test { get; set; }
}

For some reason EF does not recognise that the TestGrandChild.TestChildId is a foreign key to TestChild.TestChildId . If I try to force it like so:

[Table("TestGrandChild", Schema = "dbo")]
public class TestGrandChild {
    [Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int TestGrandChildId { get; set; }

    [Key, ForeignKey("TestChild"), Column(Order = 2)]
    public int TestChildId { get; set; }

    public virtual TestChild Test { get; set; }
}

I get the following error:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation: TestGrandChild_TestChild_Target_TestGrandChild_TestChild_Source: : The number of properties in the Dependent and Principal Roles in a relationship constraint must be identical.

Driving me CRAZY!!!!

TestChild has a composite primary key made of (TestId, TestParentId) . Therefore the foreign key in TestGrandChild refering to TestChild must be composite as well with the same number of parts - that's what the exception is telling.

TestGrandChild should look like this:

[Table("TestGrandChild", Schema = "dbo")]
public class TestGrandChild {

    [Key, Column(Order = 1), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public virtual int TestGrandChildId { get; set; }

    [Key, ForeignKey("TestChild"), Column(Order = 2)]
    public int TestId { get; set; }

    [Key, ForeignKey("TestChild"), Column(Order = 3)]
    public int TestParentId { get; set; }

    public virtual TestChild TestChild { get; set; }
}

TestGrandChild now has a composite key (TestGrandChildId, TestId, TestParentId) and the last two parts (TestId, TestParentId) form a composite foreign key to the primary key (TestId, TestParentId) in TestChild .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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