A lot of time has passed, but still cannot figure out how to avoid caching in DbContext.
I have attached an entity model below for some simple case to demonstrate what I mean.
The problem is that the dbcontext caching results. For example, I have the following code to request data from my database:
using (TestContext ctx = new TestContext()) { var res = (from b in ctx.Buildings.Where(x => x.ID == 1) select new { b, flats = from f in b.Flats select new { f, people = from p in f.People where p.Archived == false select p } }).AsEnumerable().Select(x => xb).Single(); }
In this case, everything is in order: I have what I want (only people with archive == false).
But if I add another request after it, for example, a request for buildings that have people who have the Archived Flag flag set, I have the following things that I really can't understand:
- my previous result, that is, res , will be added with data (there will be added faces with archived == true)
- the new result will contain absolutely everything Person, regardless of whether Archived is equal to
The following code for this request:
using (TestContext ctx = new TestContext()) { var res = (from b in ctx.Buildings.Where(x => x.ID == 1) select new { b, flats = from f in b.Flats select new { f, people = from p in f.People where p.Archived == false select p } }).AsEnumerable().Select(x => xb).Single(); var newResult = (from b in ctx.Buildings.Where(x => x.ID == 1) select new { b, flats = from f in b.Flats select new { f, people = from p in f.People where p.Archived == true select p } }).AsEnumerable().Select(x => xb).Single(); }
By the way, I set LazyLoadingEnabled to false in the TestContext constructor.
Does anyone know how to solve this problem? How can I get in my request what I really write in my linq for an object?
PS @Ladislav maybe you can help?
