javad hemati
javad hemati

Reputation: 252

Attaching an entity of type 'X' failed because another entity of the same type already has the same primary key value

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;
}

Upvotes: 13

Views: 44397

Answers (5)

Sohail Akhter
Sohail Akhter

Reputation: 53

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.

Upvotes: 2

Prime
Prime

Reputation: 89

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;

Upvotes: 2

Nitish Katare
Nitish Katare

Reputation: 1165

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.

Upvotes: 21

M. Wiśnicki
M. Wiśnicki

Reputation: 6203

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;
}

Upvotes: 0

federico scamuzzi
federico scamuzzi

Reputation: 3778

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;
}

Upvotes: 6

Related Questions