locked
How to include a property in a Class of Collection of the Same Class RRS feed

  • Question

  • User121401405 posted

    Hello,
    I would like to ask for some assistance in explaining how to create a collection property of the same class, as in the following example:

    public class Person
    {
        public int PersonId { get; set; } //Primary key
        //Other Properties. 
        public int ParentId { get; set; } //ForignKey of another Person
        public virtual ICollection<Person> People { get; set; }//Coloection of the same class.
    }

    Note: I am using EF-Code First .. How to Create a table of such structure... ?
    Many Thanks
    Tuesday, February 11, 2020 7:27 PM

All replies

  • User288213138 posted

    Hi Moonch,

    How to include a property in a Class of Collection of the Same Class

    Note: I am using EF-Code First .. How to Create a table of such structure... ?

    According to your description, I couldn’t understand your requirement clearly.

    Do you mean you want to know how to create a table base on above class in EF code first?

    If this is your requirement, I suggest you could you could refer to this link:

    https://docs.microsoft.com/en-us/ef/ef6/modeling/code-first/workflows/new-database

    In code first, DbContext worked out what classes to include in the model by looking at the DbSet properties that we defined. It then uses the default set of Code First conventions to determine table and column names, determine data types, find primary keys, etc.

    If I misunderstand your requirement, please post more details information about your requirement.

    Best regards,

    Sam

    Wednesday, February 12, 2020 4:10 AM
  • User121401405 posted

    Hello Samwu.

    My question was not about Code Fisrt DataBase, but rather how to create a table based on an entity class that includes a collection of the same class, the trick is to include a foreign key that refers to the parent class. this cant be done using data annotation, fluent Api probably,,, or perhaps only Sql query.... so meaning :

    a person can have a collection of child persons, and a child person can refer to the parent via the Parent Id (Null able foreign key)... all in the same class.

    Any Idea how to do this (either Data Annotation or Fluent Api)..........

    Thanks.

    Wednesday, February 12, 2020 10:42 AM
  • User288213138 posted

    Hi moonch,

    how to create a table based on an entity class that includes a collection of the same class

    If you want to create a table base on entity class, I think code first is the best way. you just need to create the relationships of these classes, then execute the command command, then add_migration and finally update-database.

    As far as i know, If you want to connect your database to your code, you need to go through either ef or ado.net.

    perhaps only Sql query.... so meaning :

    a person can have a collection of child persons, and a child person can refer to the parent via the Parent Id (Null able foreign key)... all in the same class.

    If you want to create a foreign key relationship table, then you can directly in the database, why should you create it based on the class?

    Best regards,

    Sam

    Thursday, February 13, 2020 7:10 AM