[Solved] InvalidOperationException: The entity type 'TableNameOrModelName' requires a primary key to be defined. If you intended to use a keyless entity type call 'HasNoKey()'.

Question: How do you solve the error: InvalidOperationException: The entity type 'ArticleGroupCount' requires a primary key to be defined. If you intended to use a keyless entity type call 'HasNoKey()'.


Please Login to see the rest of the answer

Answer: If you intend to use the Entity for Displaying Data such as Analytics, then go to your Database Context and remove the "set;" setter from the public virtual DbSet section of the code. This tells the Entity Framework to only use the Entity when getting the Data from the Database. 

[NB] Make sure you leave the get; getter on the DbSet for the above to work.

- If the above solution does not help you solve the error, then create modelBuilder.Entity in the Database Context block of code and define your Entity there. Don't forget to include "entity.HasNoKey(); " inside the modelBuilder.Entity block of code.


Write an Article
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.


© 2022 - ErnesTech - Privacy