我想要映射與同一個實體的多對多關係。該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 –