how to annotate a parent-child relationship with Code-First

后端 未结 4 1085
南笙
南笙 2021-01-31 19:27

When using the CTP 5 of Entity Framework code-first library (as announced here) I\'m trying to create a class that maps to a very simple hierarchy table.

Here\'s the SQL

4条回答
  •  粉色の甜心
    2021-01-31 20:20

    In Entity Framework 6 you can do it like this, note public Guid? ParentId { get; set; }. The foreign key MUST be nullable for it to work.

    class Person
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public Guid? ParentId { get; set; }
        public virtual Person Parent { get; set; }
        public virtual ICollection Children { get; set; }
    }
    

    https://stackoverflow.com/a/5668835/3850405

提交回复
热议问题