13

ErrorMessage :

Attaching an entity of type 'FaridCRMData.Models.Customer' 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.

My Code:

public class FactorController : Controller
{
    public JsonResult SaveFactor(Factor factor,int id)
    {
        if (id > 0)
        {
            bool result = new FactorService.BaseService.Update(factor);
            return new JsonResult() { Data = result };
        }

    }
}

FactorService.BaseService.cs :

public bool Update(TEntity entity)
{
    var entry = context.Entry(entity);
    if (entry.State == EntityState.Detached || entry.State == EntityState.Modified)
    {

        context.Set<TEntity>().Attach(entity);// Error Is Here
        entry.State = EntityState.Modified;
        context.SaveChanges();
    }
    return true;
}
joshmcode
  • 3,471
  • 1
  • 35
  • 50
javad hemati
  • 236
  • 1
  • 2
  • 6
  • 1
    The issue is that the object already exists in the context with that Id. You don't need to **Attach** it if it is already there. – Aggromonster Jun 01 '17 at 19:00

5 Answers5

21

I believe you might have invoked Select before the update, By default, DBContext will cache the record when they are fethced (Selected), use "AsNoTracking()" in your select call while fetching record.

Nitish Katare
  • 1,147
  • 2
  • 12
  • 22
  • 1
    This is the most appropriate answer. – Prashant Agarwal Jan 02 '19 at 22:19
  • 1
    This is the answer I've been looking for all day! Here is my example, in case it helps someone else figure out where it should go. Ex: `var recordToUpdate = db.Reservations.AsNoTracking().Single(x => x.ID == reservation.ID);` the next few lines are the values in the record that I did not want to update on the edit page, then the `db.Entry(reservation).State = EntityState.Modified;` and `db.SaveChanges();` – Jamie Apr 22 '19 at 20:36
6

have you tried to mark your entity as modified BEFORE attach it to the context?

like this:

public bool Update(TEntity entity)
{
    var entry = context.Entry(entity);
    if (entry.State == EntityState.Detached || entry.State == EntityState.Modified)
    {
        entry.State = EntityState.Modified; //do it here

        context.Set<TEntity>().Attach(entity); //attach

        context.SaveChanges(); //save it
    }
    return true;
}
federico scamuzzi
  • 3,708
  • 1
  • 17
  • 24
  • hapy help you!!..the mistery of EF ;-) – federico scamuzzi Jan 16 '18 at 12:39
  • 1
    Well, if the first statement (`entry.State = EntityState.Modified`) succeeds, after that the entry is attached. How would EF set the state of an unattached entry? And why `|| entry.State == EntityState.Modified`? What's the point of setting a `Modified` entity to `Modified`? In other words, *only* setting the entry's state to `Modified` is enough. It's more useful to check if the entry isn't `Deleted`. – Gert Arnold Nov 02 '18 at 22:06
  • For some reason I needed to move the `State=EntityState.Modified` to the beginning of the function, before I actually modified anything. If it was at the end near SaveChanges, it would crash everytime. – James L. Jun 19 '20 at 15:49
2

If you are selecting some data in between Update operation (as i was getting problem). Don't use .ToList() or .Find() .... Instead of that use as IQueryable , then use .AsNoTracking()..... Problem solved.

Sohail Akhter
  • 55
  • 1
  • 9
1

You need to detach the local version first and then do the update process

var localEntity = dbContext.Set<theModel>()
    .Local
    .FirstOrDefault(f => f.Id == theModel.Id);
if (localEntity != null)
{
    dbContext.Entry(localEntity).State = EntityState.Detached;
}
dbContext.Entry(appModel).State = EntityState.Modified;
Prime
  • 69
  • 2
  • 12
0

Try simple change state for entity wich you trying update and SaveChanges()

public bool Update(TEntity entity)
{

        context.Entry(entity).State = System.Data.EntityState.Modified;
        context.SaveChanges();
        return true;
}
M. Wiśnicki
  • 6,094
  • 3
  • 23
  • 28