Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Temporary Value Error During Entity Framework Core Modify

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.

like image 427
kf7ags Avatar asked Apr 29 '19 23:04

kf7ags


4 Answers

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

like image 85
rlv-dan Avatar answered Nov 14 '22 00:11

rlv-dan


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

like image 34
Muhammad Moid Shams Avatar answered Nov 14 '22 00:11

Muhammad Moid Shams


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.
like image 7
Saim Avatar answered Nov 14 '22 02:11

Saim


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;
}
like image 2
Tomino Avatar answered Nov 14 '22 01:11

Tomino