I'm using Entity Framework to handle data transactions and we have a user entity which had a primary key type integer named ID
.
In our SQL Server the Identity
specification was set to Yes
, Is Identity
also set to yes with an auto increment of 1.
To delete a user, I used this code:
ctx.Users.Attach(user);
ctx.Entry(user).State = EntityState.Deleted;
ctx.SaveChanges();
However, we have changed our user entity to have a Guid
as a primary key:
public class User
{
DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public Guid ID { get; set; }
public string Username { get; set; }
}
In the database, it is set to data type uniqueidentifier
with a "Default Value or Binding" set to (newsequentialid())
After changing this, the Entity Framework code to delete a user throws the following error:
Attaching an entity of type 'Administration.Model.User' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate.
Why is it throwing this error? I suppose it has something to do with the way the primary key is generated, but how can I fix this?
Of course, I could use a linq query and use .Remove
, but I want to keep the solution I had using an int value as primary key.