SqlException: Cannot insert the value NULL into column 'Id', table 'dbo.TableName'; column does not allow nulls. INSERT fails. The statement has been terminated

Question: Why is Entity #Framework throwing an error that "#SqlException: Cannot insert the value NULL into column 'Id', table 'dbo.TableName'; column does not allow nulls. #INSERT fails. The statement has been terminated"?


Please Login to see the rest of the answer

Answer: Make sure that the Id Column Identity Specification in SQL #Server Management has value (Is Identity) set to "Yes". This lets the SQL Server #Database #Engine know to auto increment the Identity #Column which in this case is the "Id" column.



Another thing to check is the declaration or specification of the Entity Model, make sure you are specifying that the column called Id is the Key by annotating in the Model/Entity Class with a declarative of [Key] on top of the property.






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