How to delete objects within a list?

Asked

Viewed 964 times

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

  • You need to show the models that make relationship with customers.

  • @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.

  • @LINQ Do I need to give a include on all objects that depend on the client object? I don’t quite understand the comment.

  • Ah, you want to set up a delete Cascade for dependent Cliente?

  • @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?

  • You got it, baby

  • How is configured your Dbcontext ? see your Onmodelcreating method if it is configured not to delete in Cascade. modelBuilder.Conventions.Remove<Manytomanycascadedeleteconvention>();

  • @Marconciliosouza There’s nothing in it but the standard "throw new Unintentionalcodefirstexception()".

Show 4 more comments

1 answer

1

By default Entityframework already does the deletecascade by default, only if you turn it off with the following command it will not do it:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
}

In your case you have a relationship 1-0:1 thus the Entityframework doesn’t know what to do when it has a value null, it cannot delete. This is a limit of EntityFramework < 6 and has nothing to do

Suggestion:

  1. As informed, delete child records manually.
  2. There are several libraries that extend the Entityframework as the http://entityframework-plus.net/ try to find something that will do this.

About the 2 commands you executed In entity 6.0 there is a difference between:

context.Cliente.Remove(entity);

and

context.Entry(Cliente).State = EntityState.Deleted;

When using the first cascade are enabled, EF will internally perform the deletions required for the child records. When using the second option, the EF will not handle the required deletions.

Browser other questions tagged

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