Using SQL and EF Core, I need to efficiently fetch data scattered across many tables.
There are ~50 tables, with many tables having multiple FK relationships, and the layers of FK relationships run up to about 5 layers deep. Almost all are one-to-many where "many" is less than 10. There is one table that all FK relationships eventually flow back to, and which defines the subset of data (<4MB) that a discrete set of calculationsworks on (consider it a unit of work in the business domain) .
Simplistically I could load that one parent entity and -- via a ridiculous number of .Include() and .ThenInclude() LINQ statements -- I could get all the dependent data I need. I could run the calculations, make sure everything is okay, and then SaveChanges() to commit a coherent and correct unit of work back to the database.
However the ThenInclude() approach explodes into slow queries shockingly quickly, and so I find myself calling context.Entry(Foo).Collection(x => x.Bars).Load() many times within loops to fetch data just before it is needed. All those small trips to the database are making me queasy, although so far in my dev-scale database it seems to be working fine. Perhaps is spreads out the load nicely. (Famous last words, I fear.)
- Do you have any EF advice (relative to my ThenInclude() and Load() mentions) for efficiently fetching data scattered across dozens of tables to build my scope of calculations?
- Thinking ahead to Azure SQL latency and async/await, how should I adjust my approach?
Thanks in advance.