4
I have the following code:
using (Context contexto = new Context())
{
List<Cliente> listCliente = contexto.Cliente.Where(x => x.Status == 0).ToList();
}
How do I delete this result from the database? Remembering that I have numerous relationships that I would also like to delete.
Something like:
using (Context contexto = new Context())
{
List<Cliente> listCliente = contexto.Cliente.Where(x => x.Status == 0).ToList();
foreach(Cliente objCliente in lilstCliente)
{
contexto.Cliente.Remove(objCliente);
}
contexto.SaveChanges();
}
When rotating gives the following error:
The Relationship could not be changed because one or more of the Foreign-key properties is non-nullable. When a change is made to a Relationship, the Related Foreign-key Property is set to a null value. If the Foreign-key does not support null values, a new Relationship must be defined, the Foreign-key Property must be Assigned Another non-null value, or the unrelated Object must be Deleted.
Instead of:
contexto.Cliente.Remove(objCliente);
I tried too:
System.Data.Entity.Infrastructure.DbEntityEntry deb = contexto.Entry(entity);
deb.State = EntityState.Deleted;
And made the same mistake.
Updating the case:
Well, since I haven’t figured out how to do this cascading exclusion in a generic way and I need to put it into production, for now I’m treating object for object, and since I probably haven’t predicted every possible relationship, I have made so that when there is some foreign key error, serious this error and I know I need to do an extra treatment, just update the service in question.
The object I want to exclude can have multiple relationships, and these relationships can have other relationships, all depending on the object I want to exclude. Ex: I want to delete client, client has a process, process has history. That is, if I exclude the client, I want both the process and the process history to be deleted as well.
Try to understand this here : https://stackoverflow.com/questions/5538974/the-relationship-could-not-be-changed-because-one-or-more-of-the-foreign-key-pro. I’m running out of time right now, if I wasn’t trying to help you better, but it seems to resolve there. Hug
– Bruno
You need to show the models that make relationship with customers.
– Jéf Bueno
@Bruno In the case of the link you passed, it needs to do a treatment on each object that depends on the main object, right? So, I’d like to know if you can sort a delete Scade. All objects that depend on the main object would need to be deleted.
– Yoshitani
@LINQ Do I need to give a include on all objects that depend on the client object? I don’t quite understand the comment.
– Yoshitani
Ah, you want to set up a delete Cascade for dependent
Cliente
?– Jéf Bueno
@LINQ More or less so, but by c# or some functionality of the Entity framework, is it possible? Not by the database or edmx, you understand?
– Yoshitani
You got it, baby
– Jéf Bueno
How is configured your Dbcontext ? see your Onmodelcreating method if it is configured not to delete in Cascade. modelBuilder.Conventions.Remove<Manytomanycascadedeleteconvention>();
– Marco Souza
@Marconciliosouza There’s nothing in it but the standard "throw new Unintentionalcodefirstexception()".
– Yoshitani