简体   繁体   中英

Stop related entities being loaded automatically in Sqlite in-memory dB using Entity Framework Core

I'm using EF Core (v1.1.3) and C#.

In my unit tests I create a sqlite in-memory database using the code-first db model, and then populate it with some test data. However when loading a list of entities from the dB, then related entities are getting populated, despite no '.Include' being used.

If I use the same model to create an actual on-disk database and the do the same load, then the related entities only get loaded if I use an .Include .

Is this a bug in the sqlite in-memory implementation (v1.1.0), or am I missing something?

I have already checked that there is nothing being tracked in the database, and obviously I can't dispose the context and renew it because of it being in-memory, and I'm limited to this version of EF & Sqlite currently, due to legacy reasons.

Obviously I'd prefer to have an in-memory option for unit tests rather than start creating on-disk tests, for the performance.

I had exactly the same probleam, what you need to do is Detached the entity that you have created for the test. Only then when you try to read the entity again, EF will fetch it from DB.

write this line after you create new entity: dbContext.Entry(entity).State = EntityState.Detached;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM