1

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
randomizer
  • 1,619
  • 3
  • 15
  • 31
  • Did you check in the db if some users have empty guid – Saravanan Mar 26 '15 at 06:16
  • You might have a look at my answer on [ASP.NET MVC - Attaching an entity of type 'MODELNAME' failed because another entity of the same type already has the same primary key value](http://stackoverflow.com/questions/23201907/asp-net-mvc-attaching-an-entity-of-type-modelname-failed-because-another-ent/39557606#39557606). – Murat Yıldız Sep 18 '16 at 12:38

0 Answers0