I need to load a model, existing of +/- 20 tables from the database with Entity Framework.
So there are probably a few ways of doing this:
- Use one huge Include call
- Use many Includes calls while manually iterating the model
- Use many IsLoaded and Load calls
Here’s what happens with the 2 options
-
EF creates a HUGE query, puts a very heavy load on the DB and then again with mapping the model. So not really an option.
-
The database gets called a lot, with again pretty big queries.
-
Again, the database gets called even more, but this time with small loads.
All of these options weigh heavy on the performance. I do need to load all of that data (calculations for drawing).
So what can I do?
a) Heavy operation => heavy load => do nothing 🙂
b) Review design => but how?
c) A magical option that will make all these problems go away
When you need to load a lot of data from a lack of different tables, there is no “magic” solution which makes all problems go away. But in addition to what you have already discussed, you should consider projection. If you don’t need every single property of an entity, it is often cheaper to project the information you do need, i.e.:
One other thing to keep in mind is that for very large queries, the cost of compiling the query can often exceed the cost of executing it. Only profiling can tell you if this is the problem. If this turns out to be the problem, consider using CompiledQuery.