[AspNet 5 Entity Framework 6 Error] InvalidOperationException: The property "ModelName.Id" has a temporary value while attempting to change the entity's state to "Modified". Either set a permanent value explicitly, or ensure that the database is configured to generate values for this property.

Error: InvalidOperationException: The property "ModelName.Id" has a temporary value while attempting to change the entity's state to 
                   "Modified". Either set a permanent value explicitly, or ensure that the database is configured to generate values for this property.

Solution:
Make sure that you are not attempting to modify or access the tracked Entity that has been given a temprory ID value.
Good rule of thumb is to save the Changes to the Context in the Caller Function in the Controller.

This will make sure that whatever the changes made to the Entity are persisted and the Context can allow access or modification to the Entity since the previous changes have been committed, otherwise the error might occur.

- In the Controller Action, after passing the request to the Repository, don't save the changes to the Context in the Repository Class modifying the Entity. Instead wait unitl the response from the Repository modifying the Entity comes back that is when you would go ahead and Save Changes to the Context.
This way the error is not thrown.


Edit this Article
ebook e-book InvalidOperationException: The property "ModelName.Id" has a temporary value AspNet 6 Error .NET 5 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