Entity Framework 6 Database First Doesn't Generate Entity on Table With Composite Key

composite-primary-key ef-database-first entity-framework entity-framework-6 sql-server

Question

Suppose I have the following table definition with a composite primary key:

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])
);

When I update the model to include this table, Entity Framework fails to generate the entity class. Is this due to the composite primary key? Is there any way to make Entity Framework generate the entity class?

1
2
10/13/2017 7:32:13 PM

Accepted Answer

Gert Arnold's comment pointed me in the right direction, as did this answer.

Once I added another column besides the two primary keys, Entity Framework generated the entity. Here is a sample table definition for which EF Database First will create an entity:

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])
);
3
10/13/2017 8:06:11 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow