【发布时间】:2012-02-14 04:25:26
【问题描述】:
我首先尝试在 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; }
}
一个帐户可以拥有多个产品。
但是 EF 约定将创建 DB 表为:
Products Table
--------------
Id
Name
Account_Id <- What is this?
Accounts Table
--------------
Id
Name
这看起来不像是多对多的表结构?如何配置 fluent API 以反映关系并创建中间表:
AccountProducts Table
---------------------
Account_Id
Product_Id
【问题讨论】:
-
这不是你想要的多对多吗?
-
谢谢 - 编辑标题以反映正确的含义
-
格式正确的问题。容易理解。
标签: c# entity-framework-4.1 ef-code-first associations