Search code examples
wpfdatagridentity-framework-6

Entity Framework context not clearing between where clauses


I'm using EF 6.0 with WPF. I initilaly load my data to a datagrid with one where clause condition. Then the user can change the where clause from the UI. What is happening is the first where clause on load works fine. Then when the second where clause is executed the data is added to the first local context. Is there a way to clear the local context so that when another query is performed the context does not contain the data from both?

I tried to detach the context but I'm getting an error: _context.Entry(abc).State = EntityState.Detached;

try
{
   _context.myData.Where(x => x.myFlag == true).Load();
}

catch (Exception except)
{
    // error
}

myVSource.Source = _context.myData.Local;

\\ then I have a click even which chnages the where clause

private void RefreshData_Click(object sender, RoutedEventArgs e)
{
    _context.myData.Where(x => x.myFlag == false).Load();
}


Solution

  • Actually, I found a solution. In the click event, I simply added:

    _context.Set().Local.ToList().ForEach(x => _context.Entry(x).State = EntityState.Detached);

    before _context.myData.Where(x => x.myFlag == false).Load(); and it cleared the current data displayed.