I have the following code:
[HttpPost]
public ActionResult Eliminar(Usuario usuario)
{
db.Usuarios.Attach(usuario);
usuario.Transacciones.ToList().ForEach(t => db.Transacciones.Remove(t));
usuario.Eventos.ToList().ForEach(e => db.Eventos.Remove(e));
db.Usuarios.Remove(usuario);
db.SaveChanges();
return RedirectToAction("Index");
}
I can't make it work. I know that to delete an entity you first have to attach it, but it isn't working for an entity that has relations.
I've also tried to do a foreach loop, and attaching each Transaccion and Evento entities before removing them, but it doesn't work neither.
This is the error the InnerException contains:
System.Data.SqlClient.SqlException: The DELETE statement conflicted with the REFERENCE constraint "FK_TR_US". The conflict occurred in database "bd_dm", table "dbo.Transacciones", column 'idUsuario'. The statement has been terminated.
I know what that means, but I don't know how to make the code work. I need to remove first all the Transacciones and Eventos related to the Usuario, to be able to remove the Usuario.
After a lot of debugging (in a remote server) I found out what the problem was. The Usuario usuario data is passed to the Eliminar method correctly, but the related objects are not. So I have to load them before being able to remove them, and then remove the Usuario object.
This is my final code:
[HttpPost]
public ActionResult Eliminar(Usuario usuario)
{
db.Usuarios.Attach(usuario);
db.Entry(usuario).Collection("Transacciones").Load();
db.Entry(usuario).Collection("Eventos").Load();
usuario.Transacciones.ToList().ForEach(t => db.Transacciones.Remove(t));
usuario.Eventos.ToList().ForEach(e => db.Eventos.Remove(e));
db.Usuarios.Remove(usuario);
db.SaveChanges();
return RedirectToAction("Index");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With