[Solved] System.InvalidOperationException: 'Unable to track an entity of type 'EntityName' because its primary key property 'Id' is null

Question: System.InvalidOperationException: 'Unable to track an entity of type 'EntityName' because its primary key property 'Id' is null.


Please Login to see the rest of the answer

Answer: In the Database Context add  entity.Property(e => e.Id).ValueGeneratedOnAdd()
- After adding this to the Entity Property you might have an error that says: Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert explicit value for identity column in table 'Table' when IDENTITY_INSERT is set to OFF.

when this error happens: 


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