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

.net c# entity-framework entity-framework-6 repository-pattern

Question

I'm stuck at the following location where my code throws an exception:

dbContext.Entry(entity).State = EntityState.Modified;

Although I've been using this code for a month, this error just came up two days ago. I've read a lot of posts, but none have been helpful. The issue is that if you attempt to alter an existing record, the following error is displayed:

Attaching an entity of type 'X' 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.

I used the code provided below:

{
    if (unitOfWork.AddressRepository.DataSet.Where(x => x.AddressId == item.AddressId).ToList().Count() > 0)
       unitOfWork.AddressRepository.Update(item);
    else
       unitOfWork.AddressRepository.Create(item);
}  
public virtual void Update(T entity)
{
    dbContext.Entry(entity).State = EntityState.Modified;
} 

I also conducted an experiment using the "Update" approach for testing purposes.

try
{
    dbContext.Entry(entity).State = EntityState.Modified;
}
catch (Exception ex)
{
    Create(entity);
}

public virtual void Create(T entity)
{
    dbSet.Add(entity);
}

//constructor taking the database context and getting the appropriately typed data set from it
public Repository(DBEntities context)
{
    dbContext = context;
    dbSet = context.Set<T>();
}

My dbContext is filled up in the function Object() { [native code] }. As a result, I always start with dbContext.

Each time A new record was created via the execution of the Create(entity) method. I discovered duplicate entries when I examined the database.

1
0
3/28/2018 4:15:08 PM

Accepted Answer

I have found the answer after two days, and I want to share it with you to assist others.

Actually, I called entity.modified after filling the model with the client's data via web apis. However, entity framework only makes changes to its current dbContext. Therefore, I first get specific data from dbContext before updating that specific object using "AutoMapper". Everything is explained in the code below.

{
   //--get particular data from dbContext 
   var _address = unitOfWork.AddressRepository.GetById(address.AddressId);
   Mapper.CreateMap<Data.Concrete.Address, Data.Concrete.Address>();
   //--update that particular object via autoMapper
   var _customAddress = Mapper.Map<Address, Address>(address, _address);
       if (unitOfWork.AddressRepository.DataSet.Where(x => x.AddressId == address.AddressId).ToList().Count() > 0)
          unitOfWork.AddressRepository.Update(_customAddress);
       else
          unitOfWork.AddressRepository.Create(address);
}
1
3/30/2018 1:47:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow