我试图首先在EF代码中建立多对多关系,但默认约定是错误的.以下类描述了这种关系:
class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
class Account
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
一个帐户可以有许多产品.
但是,EF约定将创建DB表,如下所示:
Products Table
--------------
Id
Name
Account_Id <- What is this?
Accounts Table
--------------
Id
Name
Run Code Online (Sandbox Code Playgroud)
这看起来不像是一个多对多的表结构?如何配置流畅的API以反映关系并创建中间表:
AccountProducts Table
---------------------
Account_Id
Product_Id
Run Code Online (Sandbox Code Playgroud) 我最近来到了这个班级ManyNavigationPropertyConfiguration<TEntity, TTarget>
,在那个班级里我发现了一个名为WithMany()2重载的方法.
第一次过载:
WithMany()
将关系配置为很多:很多在关系的另一侧没有导航属性.
第二次过载:
WithMany(Expression<Func<TTarget, ICollection<TEntity>>>)
将关系配置为多个:许多关系的另一侧具有导航属性.
现在是我的问题,为什么你要将关系配置为很多:许多没有导航属性(第一次重载)?我没有看到任何有用的场景......有什么想法吗?