Remove() from Entity Framework 6 only works when recreating the connection

Asked

Viewed 125 times

0

I am creating an EF6 application and I came across a problem. I am using the concept of repository and when I run a delete (context.remove(obj)) and after that I reload the list and datagrid, I realize that delete has not yet taken effect. Then delete is only noticed when I close and open the screen again, I assume this occurs because the connection is recreated.

Below is the code responsible for the operation:

public void SalvarTodos()
    {
        _contexto.SaveChanges();
    }

    public void Excluir(Func<TEntity, bool> predicate)
    {
        _contexto.Set<TEntity>().Where(predicate).ToList().ForEach(del => _contexto.Set<TEntity>().Remove(del));
    SalvarTodos();
    }

Someone can help me?

EDIT:

Repository method that reloads data (returns a Iqueriable for later Tolist()):

public IQueryable<TEntity> GetAll()
    {
        return _contexto.Set<TEntity>();
    }

EDIT 2:

Exclusion call code in Viewmodel:

public void CarregaUsuarios()
    {
        Usuarios = new BindingList<Usuario>(_app.ObterTodos().ToList());
    }

    public bool CanDelete(object args)
    {
        return (usuarioSelected != null);
    }

    public void Delete(object args)
    {
        _app.Excluir(usuarioSelected.UsuarioId);
        CarregaUsuarios();
    }

The _app.Get All command noted above has the following code, in the Application layer:

public IQueryable<Usuario> ObterTodos()
    {
        return _repositorio.GetAll();
    }
  • How is the selection made? How do you build your grids?

  • @Ciganomorrisonmendez I am using the standard MVVM with WPF, and a Bindinglist<Usuario> in Viewmodel... Step a lambda predicate as parameter to the Delete method:

  • The problem does not seem to be in the EF. It seems to me more the memory of the Grids that is still holding the old records.

  • @The interesting thing is that when I call the method that recharges the data, the object Iqueriable.Tolist() returns the same amount of records. Therefore, I assume there is some transaction trapped (even after calling context.Savechanges()), unless I am mistaken and a Tolist in a Iqueriable object does not run the query in the database again. I will edit the issue with the repository method that reloads the data.

  • @I really checked again that it is being deleted immediately at the command and the reloading method is also OK, returning all but reg. deleted. Certainly then the problem is in Binding. I updated the issue with more code, demonstrating how Binding is being done...

  • Well, it seems to me that right after deleting you need to renew Binding, right?

  • @Gypsy omorrisonmendez solved the problem. The Binding was being renewed, but was forgetting to call the method Notifypropertychanged with the name of the list of users... So really the problem was in my view and not in the Entity. Thank you very much!

Show 2 more comments
No answers

Browser other questions tagged

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