Temporary Value Error During Entity Framework Core Modify

21,818

Solution 1

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

Solution 2

This exception can also happen if you try to add an entity with a foreign key that does not exists.

Solution 3

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

  1. Make sure the "ID" property exists in the form.
  2. If it exists, then check if you have disabled it? if yes don't disable it. it won't pass the data when you post it.
  3. (If ID is primary key) Make sure ID arrives at the Update method safely without being changed.

Solution 4

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;
}
Share:
21,818
kf7ags
Author by

kf7ags

Updated on May 26, 2021

Comments

  • kf7ags
    kf7ags almost 3 years

    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.

  • kf7ags
    kf7ags almost 5 years
    I'm sorry, I don't know what the "Actions View" is. I did try that though and am still getting the exception error before I reach any save statement. If you asking for the .cshtml file and .cs file that uses this method it can be viewed at: link and: link
  • Muhammad Moid Shams
    Muhammad Moid Shams almost 5 years
    You are not binding id which in your edit page.cshtml as your id field is int and its assigning '0' in your update method
  • paulsm4
    paulsm4 over 3 years
    As Muhammad Moid Shams said, the problem is "You are not binding id". A better solution might be to add <input type="hidden" asp-for="updatedUser.ID" /> under <form> in your Edit.cshtml.
  • spatanx
    spatanx over 3 years
    Your response is the solution. The error is actually self explanatory. I just encounter thesame error. What i did was to include the ID field in the form and pass the value. That actually solved the problem
  • Eternal21
    Eternal21 over 3 years
    Thanks for this, ran into the same issue using AddRange. Would've been a nightmare to figure it out myself.
  • El.Hum
    El.Hum almost 3 years
    marcelo i just translated your answer with google. you should answer the question in english so most of us can use your helpful ideas and solutions. thank you.
  • Shad
    Shad about 2 years
    I was passing in the whole object. When I changed the route to take the ID, then fetch the object before deleting it, it worked.
  • DeveloperDan
    DeveloperDan almost 2 years
    Moving the hidden ID input within the <form> tag fixed the problem for me.