I was following along in a tutorial for ASP.NET Core and Entity Framework Core. I am able to connect to my Microsoft SQL Server database and get and add rows, but I cannot update them. My update method attaches a new user and then sets the state to modified.
Every time though I get the error:
System.InvalidOperationException: 'The property 'Id' on entity type 'UserData' 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.'
I have tried setting the primary key property to database generated, and did my best to implement that in the database, but I'm not sure it's working. I also tried running a SQL update with the FromSQL method, but nothing changed (I have a similar and probably separate problem when I try to delete).
Here is the update method from the db context implementation:
public UserData Update(UserData updatedUser)
{
var entity = db.Users.Attach(updatedUser);
entity.State = EntityState.Modified;
//db.Users.FromSql<UserData>($"UPDATE Users SET user_name={updatedUser.user_name}, first_name={updatedUser.first_name}, last_name={updatedUser.last_name}, age={updatedUser.age}, email={updatedUser.email} WHERE");
return updatedUser;
}
I do a save changes in a different method that's called directly after this one.
Here is my data model:
public class UserData
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Int64 Id { get; set; }
public string user_name { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public int age { get; set; }
public string email { get; set; }
}
Basically I just want to update a user with new information and I'm trying to use the attach and entity. Modify method used in the tutorial I'm following, but if there is a better way I'm open to that too.
EDIT: the solution is in the comment I marked as the answer. On my .cshtml page I had not bound the Id of the user to an input field and so it was being set to "0" by default.
This exception can also happen if you try to add an entity with a foreign key that does not exists.
Try
public UserData Update(UserData updatedUser)
{
var entity = db.Users.Attach(updatedUser);
entity.Entry(updatedUser).State = EntityState.Modified;
entity.SaveChanges();
return updatedUser;
}
If not worked for you then kindly share your Page.cshtml.
You might be changing the primary key value while updating the row
This "Temporary Value Error During Entity Framework Core Modify" Happens when ID does not arrive at the Update Method.
Problem is not in the code. The problem is in the view you haven't attached it here but look for the following things in your "VIEW".
I got the same exception when I had unique index, AddRange failed on unique index and then inside catch exception block was try to remove whole inserted. (Not my code, but I had to fix it :-) )
Code sample (simplified):
try {
context.AddRange(users); // List<User>, has property List<Contact>
context.SaveChanges(); // throws exception on unique index
} catch (Exception ex) {
context.RemoveRange(users); // this throws exception "The property 'UserID' on entity type 'Contact' has a temporary value"
throw;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With