InvalidOperationException: The property 'EntityName' has a temporary value while attempting to change the entity's state


Question: InvalidOperationException: The property 'EntityName' 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.
Testing:


InvalidOperationException: The property 'EntityName' has a temporary value while attempting to change the entity's state

Edited Version 2

Question
InvalidOperationException
The property 'EntityName' 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.


Answer
There is a chance that you are creating a new object and then using Entity Framework to update the current tracked Entity, this is a No-No, you can not create a new object without an ID then try to update that same newly created Entity using _context.MyEntity.Update(instanceOfNewlyCreatedObject);

The above does not make sense, instead of code your instruction as below


//Retrieve the Entity you want to modify
MyEntityNameHere entityName = _context.MyEntityNameHere.Find(id);

//Then modify as needed
entityName.Property = "MyNewPropertyValue";
_context.SaveChanges();//This works as well, as objects are tracked.




Jack said:

Has anyone found the answer yet?

Posted On: July 24, 2021 10:30:18 AM

For peering opportunity Autonomouse System Number: AS401345 Custom Software Development at ErnesTech Email AddressContact: [email protected]