I'm new and I hope you can help me figure this out, thanks in advice to everyone. Let's say I've got these 3 models
public class Especialidad
{
public int IdEspecialidad { get; set; }
public string Descripcion { get; set; }
public List<MedicoEspecialidad> MedicoEspecialidad { get; set; }
}
public class Medico
{
public int IdMedico { get; set; }
public List<MedicoEspecialidad> MedicoEspecialidad { get; set; }
}
public class MedicoEspecialidad
{
public int IdMedico { get; set; }
public int IdEspecialidad { get; set; }
public Medico Medico { get; set; }
public Especialidad Especialidad { get; set; }
}
and I configured the relationships as following:
modelBuilder.Entity<MedicoEspecialidad>().HasKey(x => new {x.IdMedico, x.IdEspecialidad});
modelBuilder.Entity<MedicoEspecialidad>().HasOne(x => x.Medico)
.WithMany(p => p.MedicoEspecialidad)
.HasForeignKey(p => p.IdMedico);
modelBuilder.Entity<MedicoEspecialidad>().HasOne(x => x.Especialidad)
.WithMany(p => p.MedicoEspecialidad)
.HasForeignKey(p => p.IdEspecialidad);
The problem that I've got is that when I delete Especialidad I can't edit/delete Medico cause it has no Especialidad and it's trying to Remove the relationship. And also it's not possible to delete Especialidad without having it assigned to a Medico because it's trying to delete the relationship and it's not found.
this is the Delete method in EspecialidadController:
public async Task<IActionResult> Delete(int id)
{
var medicoEspecialidad = await _context.MedicoEspecialidad
.FirstOrDefaultAsync(me => me.IdEspecialidad == id);
_context.MedicoEspecialidad.Remove(medicoEspecialidad);
await _context.SaveChangesAsync();
var especialidad = await _context.Especialidad.FindAsync(id);
_context.Especialidad.Remove(especialidad);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
this is the delete method in MedicoController:
public async Task<IActionResult> DeleteConfirmed(int id)
{
var medicoEspecialidad = await _context.MedicoEspecialidad
.FirstOrDefaultAsync(me => me.IdMedico == id);
_context.MedicoEspecialidad.Remove(medicoEspecialidad);
await _context.SaveChangesAsync();
var medico = await _context.Medico.FindAsync(id);
_context.Medico.Remove(medico);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
this is the Edit method in MedicoController
public async Task<IActionResult> Edit(int id, [Bind("IdMedico,Nombre,Apellido,Direccion,Telefono,Email,HorarioAtencionDesde,HorarioAtencionHasta")] Medico medico, int IdEspecialidad)
{
if (id != medico.IdMedico)
{
return NotFound();
}
if (ModelState.IsValid)
{
try
{
_context.Update(medico);
await _context.SaveChangesAsync();
var medicoEspecialidad = await _context.MedicoEspecialidad
.FirstOrDefaultAsync(me => me.IdMedico == id);
_context.Remove(medicoEspecialidad);
await _context.SaveChangesAsync();
medicoEspecialidad.IdEspecialidad = IdEspecialidad;
_context.Add(medicoEspecialidad);
await _context.SaveChangesAsync();
}
You have to remove the record from mm MedicoEspecialidad table and from main table in one transaction, so use SaveChanges only ones. And you will have to check if a MedicoEspecialidad table has a record before trying to remove from this table
public async Task<IActionResult> Delete(int id)
{
var medicoEspecialidad = await _context.MedicoEspecialidad
.FirstOrDefaultAsync(me => me.IdEspecialidad == id);
if ( medicoEspecialidad != null)
_context.MedicoEspecialidad.Remove(medicoEspecialidad);
var especialidad = await _context.Especialidad.FindAsync(id);
var result=0;
if (especialidad !=null)
{
_context.Especialidad.Remove(especialidad);
result = await _context.SaveChangesAsync();
}
if (result > 0) return RedirectToAction(nameof(Index));
return null; // or better to return an error message
}
the same logic is for another table Medico
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.