Entity Framework 未清除 where 子句之间的上下文

Entity Framework context not clearing between where clauses

我将 EF 6.0 与 WPF 一起使用。我最初将我的数据加载到带有一个 where 子句条件的数据网格。然后用户可以更改 UI 中的 where 子句。正在发生的是第一个 where 子句 on load 工作正常。然后,当执行第二个 where 子句时,数据将添加到第一个本地上下文中。有没有办法清除本地上下文,以便在执行另一个查询时上下文不包含来自两者的数据?

我试图分离上下文,但出现错误:_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();
}

其实我找到了解决办法。在点击事件中,我简单地添加了:

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

之前 _context.myData.Where(x => x.myFlag == false).Load(); 并清除了当前显示的数据。