自引用许多一对多递归关系,code第一实体框架 [英] Self-referencing many-to-many recursive relationship code first Entity Framework

查看:238
本文介绍了自引用许多一对多递归关系,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.

这篇关于自引用许多一对多递归关系,code第一实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆