自引用多对多递归关系代码优先实体框架 [英] Self-referencing many-to-many recursive relationship code first Entity Framework
本文介绍了自引用多对多递归关系代码优先实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎根本无法完成这项工作
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 First 会将单向关联视为一对多.因此,您需要使用 fluent API 让 Code First 知道您想要多对多的自引用关联:
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().Map(m =>
{
m.MapLeftKey("MemberId");
m.MapRightKey("FriendId");
m.ToTable("MembersFriends");
}
);
}
这篇关于自引用多对多递归关系代码优先实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文