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.
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>
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"); }
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.
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
– Lucas Miranda
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.
– Ramon Almeida
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?
– Leandro Angelo
Can you pass a drawing of the model? Some Enum in these entities? How is the injection of the model?
– Andre.Santarosa
I edited it, André. .
– Ramon Almeida