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

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

    It should work using a mapping like below:

    class FamilyContext : DbContext
    {
        public DbSet People { get; set; }
    
        protected override void OnModelCreating(ModelBuilder builder)
        {
            builder.Entity().HasMany(x => x.Children).WithMany().Map(y =>
                {
                    y.MapLeftKey((x => x.Id), "ParentID");
                    y.MapRightKey((x => x.Id), "ChildID");
    
                });
        }
    }
    

    However that throws an exception: Sequence contains more than one matching element. Apperently that is a bug.

    See this thread and the answer to @shichao question: http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-fluent-api-samples.aspx#10102970

提交回复
热议问题