我想要映射与同一个实体的多对多关系。该User
实体有Contacts
的IList<User>
数据字段,用于存储用户的联系人/朋友信息:实体框架核心:与同一个实体的多对多关系
public class User : DomainModel
{
public virtual IList<User> Contacts { get; protected set; }
//irrelevant code omitted
}
当我试图用流利的API映射这个多对多的关系,它给我带来些麻烦。显然,当我在user.Contacts
属性上使用HasMany()
时,它没有WithMany()
方法可以调用下一个。 Visual Studio的智能感知只显示WithOne()
,但不显示WithMany()
。
modelBuilder.Entity<User>().HasMany(u => u.Contacts).WithMany()
// gives compile time error: CS1061 'CollectionNavigationBuilder<User, User>' does not contain a definition for 'WithMany' and no extension method 'WithMany' accepting a first argument of type
那么为什么会发生这种情况呢?有什么我做错了映射这种多对多的关系?
你可以看看这个:https://ef.readthedocs.io/en/latest/modeling/relationships.html#many-to-many –