Reputation: 2631
I've switched our tests to use a SQLite :memory:
database over a database file in an effort to speed up our tests. Everything has gone smoothly, except for one caveat: it's automatically eagerly loading navigation properties on entities.
If I run the project and hit it with Postman using an actual database file, it doesn't eagerly load them, and only loads navigation properties if I specify I want them using .Include()
.
It didn't do this before I switched to a SQLite Memory database for the tests.
The DbContext configuration is as follows:
Configuration.LazyLoadingEnabled = true;
Configuration.ProxyCreationEnabled = false;
Configuration.ValidateOnSaveEnabled = true;
Has anyone run into this issue before?
Upvotes: 3
Views: 906
Reputation: 16282
This is an old post, but I thought I'd provide an answer for anyone else that stumbles upon this.
What is likely happening here is simply that your DbContext has the entities cached in its ChangeTracker. Regardless of the underlying provider, when you insert an entity into the database, Entity Framework tracks that entity. If you query for an entity you just inserted, Entity Framework assumes the entity would not have changed and therefore returns the entity from the ChangeTracker. If you changed your query to do a _myDbContext.Set<SomeEntity>().AsNoTracking().ToList()
then you should find that it performs a query and only prefetches related entities you've specified.
Upvotes: 4