Entity Framework 6: Removal method does not work

Asked

Viewed 113 times

1

I’m trying to delete an object using the Repository Pattern, but the problem is that when calling the method for removal nothing happens, nor throws exception.

Financeirocontroller.Cs:

[HttpPost]
public ActionResult DeleteConfirmed(int id)
{
    var financa = _financeiroApp.ObterFinanca(id); // Aqui retorna o objeto sem nenhuma exceção.
    _financeiroApp.Remover(financa);

    return RedirectToAction("Index");
}

Repositorybase.Cs:

public void Remover(T obj)
{
    banco.Set<T>().Remove(obj); // Não remove, mas também não lança nenhuma exceção.
    banco.SaveChanges();
}

I can not understand, because when obtaining the object it is within the context and, therefore, should be removed without any problem.

  • 1

    Ever tried to give a contexto.SaveChanges(); after removing the object?

  • I tried yes Diego, I forgot to put here. I already edited.

  • 1

    can show how your Repositorybase Class is declared ?

1 answer

1


If your goal is to delete, you can change your Remove for Deleteobject

Objectcontext.Deleteobject(Entity) marks the entity as deleted in context. (Entitystate is deleted after that.) If you call Savechanges then the Entity Framework sends an SQL DELETE statement to the database. If there are no reference restrictions in the database the entity will be deleted, otherwise an exception is thrown.

Your Entitycollection.Remove(childEntity), brand name the relationship between father and son Entity as eliminated. If the relationship has a referential integrity restriction, calling the Remove method on a dependent marks object both the relationship and the dependent object to exclusion. This is because the restriction indicates that the dependent object cannot exist without a parent relationship.

Remove returns false when the specified object is not in the collection.

Raferencia

Browser other questions tagged

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