I have the following code, where I receive an incomplete entity from the frontend, make changes and update it. Afterwards, I retrieve the same entity, now complete, from the database, make new changes and update it again... (Business rules have been removed for better understanding.)
`
public async Task<SeparationRequest> CollectSignature(SeparationRequest separationRequest)
{
await base.UpdateAsync(separationRequest);
int IdSeparationRequestAux = separationRequest.Id;
separationRequest = null;
var newSeparationRequest = await GetByIdAsync(IdSeparationRequestAux);
await base.UpdateAsync(newSeparationRequest );
return newSeparationRequest;
}
`
This is the UpdateAsync Method: `
public virtual async Task<TEntity> UpdateAsync(TEntity entity)
{
Repository.Update(entity);
await _unitOfWork.SaveChangesAsync();
return entity;
}
`
This is the GetByIdASync Method: `
public override async Task<SeparationRequest> GetByIdAsync(object id)
{
var apps = await Repository.GetFirstOrDefaultAsync(predicate: x => x.Id == (int)id, disableTracking: true,
include: x => x.AsNoTracking().Include(x => x.SeparationRequestStatus)
.Include(x => x.Itens)
.ThenInclude(x => x.SeparationRequestItemStatus));
return apps;
}
`
The GetByIdAsync method calls the following method: Get FirstOrDefaultAsync `
public virtual async Task<TEntity> GetFirstOrDefaultAsync(Expression<Func<TEntity, bool>> predicate = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
Func<IQueryable<TEntity>, IIncludableQueryable<TEntity, object>> include = null,
bool disableTracking = true,
bool ignoreQueryFilters = false)
{
IQueryable<TEntity> query = _dbSet;
if (disableTracking)
{
query = query.AsNoTracking();
}
if (include != null)
{
query = include(query);
}
if (predicate != null)
{
query = query.Where(predicate);
}
if (ignoreQueryFilters)
{
query = query.IgnoreQueryFilters();
}
if (orderBy != null)
{
return await orderBy(query).FirstOrDefaultAsync();
}
else
{
return await query.FirstOrDefaultAsync();
}
}
`
the first updateAsync is executed successfully, but when hitting the second UpdateAsync the following exception is triggered:
System.InvalidOperationException: 'The instance of entity type 'SeparationRequest' cannot be tracked because another instance with the same key value for {'Id'} is already being tracked. When attaching existing entities, ensure that only one entity instance with a given key value is attached. Consider using 'DbContextOptionsBuilder.EnableSensitiveDataLogging' to see the conflicting key values.'
Does anyone have any idea what I can do? obs: I'm using AddScopped and with AddTransient I got the same error