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

后端 未结 4 1121
南笙
南笙 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:19

        class Person
    { 
        [key()]
        public Guid Id { get; set; }
        public String Name { get; set; }
        [ForeignKey("Children")]
        public int? PersonId {get; set;} //Add ForeignKey
        public virtual Person Parent { get; set; }
        public virtual ICollection Children { get; set; }
    }
    
    builder.Entity().HasMany(m => m.Children)
                            .WithOne(m => m.Parent)
                            .HasForeignKey(m => m.PersonId);
    

提交回复
热议问题