Exception released when deleting registry using Entity Framework Core. How to resolve?

Asked

Viewed 187 times

0

When trying to delete registration of entities "Patients" and "Doctors" is returned an exception. The strange thing is that for the entity of "Schedules" everything happens normally.

Follows the exception:

An unhandled Exception occurred while Processing the request. Invalidoperationexception: The instance of Entity type 'Patient' 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. Microsoft.EntityFrameworkCore.ChangeTracking.Internal.IdentityMap.ThrowIdentityConflict(InternalEntityEntry entry)

I will use the code of the entity "Patient" as an example, but it occurs in the same way for the other entity cited.

  1. Form in the view that sends the action

    <form asp-action="Delete">
        <input type="hidden" asp-for="Id" />
        <input type="submit" value="Delete" class="btn btn-danger" /> |
        <a asp-action="Index">Back to List</a>
    </form>
    
  2. Cógigo da Controller

    public async Task<IActionResult> Delete(Guid id)
    {
    
        var pacienteViewModel = _mapper.Map<PacienteViewModel>(await _pacientesRepository.ObterPorId(id));
        if (pacienteViewModel == null)
        {
            return NotFound(); 
        }
    
        return View(pacienteViewModel);
    }
    
    [HttpPost, ActionName("Delete")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> DeleteConfirmed(Guid id)
    {
        var pacienteViewModel = _mapper.Map<PacienteViewModel>(await _pacientesRepository.ObterPorId(id));
    
        if (pacienteViewModel == null) return NotFound();
    
        await _pacientesRepository.Remover(id);
        return RedirectToAction("Index");
    }
    
  3. Code in the data access layer (Common to all, is in the abstract class inherited by all entities)

    public virtual async Task Remover(Guid id)
    {
        DbSet.Remove(new TEntity { Id = id});
        await SaveChanges();
    }
    

Recalling that the process occurs normally for the entity "Scheduling", I have analyzed the code and see nothing different.

  • 1

    in my opinion it must be related to this new Tentity {Id = id}, you are trying to create a new entity by passing the same id of an existing one

  • Yes, that’s what you can understand from the message. But I’ve done tests to see if it could be this, but nothing solved.

  • 2

    But why do you "create" a new entity to remove it? Wouldn’t it be worth finding the record and deleting it from the context?

  • Can you pass a drawing of the model? Some Enum in these entities? How is the injection of the model?

  • I edited it, André. .

1 answer

1

The problem is not the moment we try to delete, but when we get the object through EF Core before exlcuiring. The problem is because EF Core does not allow changing and deleting objects mapped by change tracking.

Follow my solution:

Problem:

    public virtual async Task<TEntity> ObterPorId(Guid id)
    {
        return await DbSet.FindAsync(id);
    }

Solution:

    public virtual async Task<TEntity> ObterPorId(Guid id)
    {

        return await DbSet.AsNoTracking()
                    .FirstAsync(c => c.Id == id);
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.