[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.






© 2024 - ErnesTech - Privacy
E-Commerce Return Policy