自引用许多一对多递归关系,code第一实体框架

自引用许多一对多递归关系,code第一实体框架

问题描述:

我似乎无法使所有这些工作

I can't seem to make this work at all

class Member
{
    public virtual IList<Member> Friends { get; set; }
    [Key]
    public int MemberId { get; set; }
    public string Name{ get; set; }
}

我尝试添加映射,但徒劳无功。有没有办法用CTP5这样做呢?

I tried adding Mappings but in vain. Is there a way to do so with CTP5?

按照惯例,code首先将采取单向协会评为一对多。因此,你需要用流利的API,让code首先知道你想有一个多对多的自我引用的关联:

By convention, Code First will take uni-directional associations as one to many. Therefore you need to use fluent API to let Code First know that you want to have a many to many self referencing association:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Member>().HasMany(m => m.Friends).WithMany();
}

请注意:有一个在CTP5一个已知的bug,不会让你定制此方案中的连接表的列名。

Note: There is a known bug in CTP5 that won't let you customize the join table column names in this scenario.