实体框架6数据库先不生成实体上表中添加复合键
问题描述:
假设我有一个复合主键下面的表定义:实体框架6数据库先不生成实体上表中添加复合键
create table [dbo].[CustomerRequests] (
[CustomerId] int not null,
[RequestId] int not null,
constraint [PK_CustomerRequests] primary key clustered ([CustomerId] asc, [RequestId] asc),
constraint [FK_CustomerRequests_Customers] foreign key ([CustomerId]) references [dbo].[Customers] ([CustomerId]),
constraint [FK_CustomerRequests_Requests] foreign key ([RequestId]) references [dbo].[Requests] ([RequestId])
);
当我更新的模式,包括本表中,实体框架无法生成实体类。这是由于复合主键吗?有没有什么办法让Entity Framework生成实体类?
答
格特阿诺德的评论指出我的方向正确,this answer。
一旦我添加了除两个主键之外的另一列,Entity Framework就会生成该实体。以下是EF Database First为其创建实体的示例表定义:
create table [dbo].[CustomerRequests] (
[CustomerId] int not null,
[RequestId] int not null,
[TimestampUtc] datetime not null,
constraint [PK_CustomerRequests] primary key clustered ([CustomerId] asc, [RequestId] asc),
constraint [FK_CustomerRequests_Customers] foreign key ([CustomerId]) references [dbo].[Customers] ([CustomerId]),
constraint [FK_CustomerRequests_Requests] foreign key ([RequestId]) references [dbo].[Requests] ([RequestId])
);
当我使用EF时,我们在代码中创建实体,然后使用迁移来创建SQL表。我认为它忽略了在SQL中手动创建的表。不完全积极,只想要我体验。 –
EF在中间无形地创建了与此表的多对多关联。如果你不想这样,给表单一个代理主键(或任何其他有意义的字段)。 –