Entity Framework - OnModelCreating not executed RRS feed

  • Question

  • User1405701641 posted

    I am trying to follow the article ASP.NET Core - Existing Database to use an existing database. I am able to generate the model classes but when I run my project I get the error "The entity type 'XXX' requires a primary key to be defined." If I place a breakpoint in Visual Studio I can see that the OnModelCreating method is never called.

    EF is forcing me to define a regular primary key. It throws an error if I try to define a composite primary key using data annotations (saying I have to use fluent api to define a composite key). But when I define a composite key using fluent api in OnModelCreating it is not getting executed and I get the error.

    If I add a primary key field to the model class then it works and OnModelCreating is executed, very strange. It seems that EF wants a regular primary key defined on all classes and doesn't like composite keys.

    Tuesday, July 25, 2017 12:36 AM

All replies