Temporary Value Error During Entity Framework Core Modify

asp.net-core c# entity-framework-core sql-server


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

4/30/2019 3:57:08 AM

Accepted Answer


    public UserData Update(UserData updatedUser)
        var entity = db.Users.Attach(updatedUser);
        entity.Entry(updatedUser).State = EntityState.Modified;
        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

4/30/2019 12:06:31 AM

Popular Answer

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.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow