1
votes

The problem that I'm having is that I can't delete a row when using entity. I'm able to find the row, but the error I get the next error:

Additional information: Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=472540 for information on understanding and handling optimistic concurrency exceptions.

I tried updating the database in the package manager console, I tried several different ways of deleting the record, but no luck by the moment. Auto Migrations are enabled.

Below is the code I am currently trying to use.

   var ctx = new Context();
   ApplicationUser user = new ApplicationUser { UserName = myUsername };
   ctx.Users.Attach(user);
   ctx.Users.Remove(user);
   ctx.Entry(user).State = EntityState.Modified;
   ctx.SaveChanges();

All help is appreciated. I have been banging my head on this for hours.

3
Is UserName a primary key of ApplicationUser entity? - Dennis
@Dennis It is not. The primary is Id. Which is a uuid. - alphamalle
it is seem to be you have an optimistic concurrency exception do you know what is that? do u have rowversion coulmn or you reading and writing from mutliple DbContexts at the same time? - Bassam Alugili
@BassamAlugili I dont believe that I have a rowversion column. It is is possible that I have multiple dbcontexts going though. I am unsure how to check. - alphamalle
This might to be a side effect. I have found this info: " I ran into this and it was caused by the entity's ID (key) field not being set. Thus when the context went to save the data, it could not find an ID = 0. Be sure to place a break point in your update statement and verify that the entity's ID has been set." - Bassam Alugili

3 Answers

3
votes

The common way to remove entity in EF is the following:

  • locate entity using DbSet<T>. This could be retrieving entity from set or attaching it to the set;
  • call DbSet<T>.Remove;
  • call DbContext.SaveChanges.

Since ApplicationUser.UserName is not a primary key property, you need to find entity. Attaching doesn't make sense here - DbSet<T>.Attach tells context to treat entity with given primary key as existing and non-modified, but you don't set primary key value (probably, it is generated in constructor, since you're using GUIDs?).

Something like this:

// assuming, that user names are not unique
var user = ctx.Users.FirstOrDefault(_ => _.UserName == myUsername);
if (user != null)
{
    ctx.Users.Remove(user);
    cts.SaveChanges();
}
0
votes

You remove your entity and after that mark it as a modified. Remove line:

ctx.Entry(user).State = EntityState.Modified;
0
votes

How about:

   var ctx = new Context();
   ApplicationUser user = new ApplicationUser { UserName = myUsername };
   ctx.Entry(user).State = EntityState.Deleted
   ctx.SaveChanges();

Or just remove line about modifing record. It is not needed, cause first of all You are deleting it not change. And second of all, .Remove function already set it to proper state.