【发布时间】:2013-01-21 22:41:57
【问题描述】:
当我的DataContext 中有两个 POCO 类(我将它们用作DbSet<>)时,EF 代码首先在幕后创建一个映射表:
class Subscription
{
ICollection<Product> Products {get;set}
}
和
class Product
{
ICollection<Subscription> Subscriptions {get;set}
}
和秘密映射表:
CREATE TABLE [dbo].[ProductSubscriptions](
[Product_Id] [bigint] NOT NULL,
[Subscription_Id] [bigint] NOT NULL
) ON [PRIMARY]
在两个 ID 字段上有一个聚集的主键。
问题: 是否可以在两个字段上都没有聚集主键的情况下创建表?因为我在这里和那里有多个条目,例如:
{ Product_Id = 1, Subscription_Id = 1 }
{ Product_Id = 1, Subscription_Id = 1 }
{ Product_Id = 1, Subscription_Id = 2 }
{ Product_Id = 1, Subscription_Id = 2 }
...
我删除了主键,但是对DataContext.Products.Subscriptions.Add(1) 的多次调用只添加了一个项目,而不是它被调用的次数。
【问题讨论】:
-
为什么 ProductSubscriptions 表中有重复项?它们是什么意思?
标签: c# sql linq entity-framework-4 ef-code-first