[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()'.

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.


Edit this Article
EntityFramework Core C Technology published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

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

Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy