Skip to content
Advertisement

Entity Framework Core “The entity type ‘XXX’ requires a primary key to be defined.”

So I’m currently trying to create a code first migration with Entity Framework Core for a table that displays which lectures the application user has completed. My model looks like this:

I want to use the UserId and the LectureId as the unique composite key. However I get this error:

The entity type ‘LectureCompletion’ requires a primary key to be defined.

I don’t understand why this is happening as I clearly have my key attributes in the correct positions? Am I able to use ApplicationUser as foreign/primary key?

Here is my Lecture model:

And my ApplicationDBContext.cs:

Advertisement

Answer

You can’t define a composite key with data annotation alone. You need to use Fluent API instead.

https://docs.microsoft.com/en-us/ef/core/modeling/keys

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement