假设我有以下带有复合主键的表定义:
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])
);
当我更新模型以包含此表时,实体框架无法生成实体类。这是由于复合主键吗?有没有办法让实体框架生成实体类?
Gert Arnold的评论为我指明了正确的方向,这个答案也是如此。
在两个主键之外添加了另一列后,实体框架生成了实体。下面是 EF 数据库优先将为其创建实体的示例表定义:
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])
);