If you must keep the context then you could detach everything before getting the next page.
foreach(var entity in db.Set<MyModel>().Local.ToList())
db.Entry(entity).State = EntityState.Detached;
Question
I'm trying to page a data set in a WPF application.
My service method looks like this:
public ObservableCollection<MyModel> GetPageOfModels(int projectId, int numSkip, int numResults)
{
this.db.Set<MyModel>()
.Where(x => x.Project.Id == projectId)
.OrderBy(x => x.Name)
.Skip(numSkip)
.Take(numResults)
.Load();
return this.db.Set<MyModel>().Local;
}
This works great the first time it is hit. But when I move to a different page, Load()
is adding the next page to the set, rather than replacing it.
If I recreate the data context before each call to GetPageOfModels
it works, but I need to keep the same data context. Is there any way to achieve this?
Solution
If you must keep the context then you could detach everything before getting the next page.
foreach(var entity in db.Set<MyModel>().Local.ToList())
db.Entry(entity).State = EntityState.Detached;