Entity Framework 6: removal method does not work

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 an exception.

FinancialController.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 why getting the object is inside the context and should be removed without any problem.

    
asked by anonymous 24.08.2016 / 21:18

1 answer

1

If your goal is to delete, you can change your Remove by DeleteObject

ObjectContext.DeleteObject (entity) marks the entity as deleted in context. (EntityState is deleted after that.) If you call SaveChanges after the Entity Framework sends a SQL statement DELETE to the database. If there are no reference constraints on the database the entity will be deleted, otherwise an exception is thrown.

Your EntityCollection.Remove (childEntity) , strong> relationship between parent and child Entity as deleted . If the relationship has a referential integrity constraint, calling the Remove method on a dependent object marks both the relationship and the bound object for deletion. This occurs because the constraint indicates that the dependent object can not exist without a relationship to the parent.

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

Raferency

    
24.08.2016 / 22:34