none
Entity with Recursive Relationship RRS feed

  • Question

  • Hi,

    How do i create a table in a database that is related to itself, using entity framework code first?

    I've done this so far:

    class Person
        {
    
            [Key]
            public int ID { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            
            public virtual Person Father { get; set; }
            public virtual Person Mother { get; set; }
            public DateTime BirthDate {get;set;}
            public bool isAlive {get;set;}
            public DateTime DeceaseDate {get;set;}
            public bool gender { get; set; }
            public string desc()
            {
                return FirstName + " " + LastName + BirthDate.Year;
            }

    please note the Mother and the Father properties.

    at runtime I get this error:

    Exception:Thrown: "Unable to determine the principal end of an association between the types 'BinaryFamily.MaL.Person' and 'BinaryFamily.MaL.Person'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations." (System.InvalidOperationException)
    A System.InvalidOperationException was thrown: "Unable to determine the principal end of an association between the types 'BinaryFamily.MaL.Person' and 'BinaryFamily.MaL.Person'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations."
    Time: 7/26/2015 12:53:21 AM
    Thread:<No Name>[15584]


    S.A.T

    • Moved by Kristin Xie Monday, July 27, 2015 8:11 AM move to EF forum
    Saturday, July 25, 2015 8:22 PM

Answers

  • Another way. Don't create the relationship in VS. Create the relationship in your database and create the Entity Framwork from the database. That might save you sometime to troubleshoot.

    chanmm


    chanmm

    Sunday, July 26, 2015 2:39 AM

All replies