EF: Why Include method is an anti-pattern IMHO? Conclusion
First, I admit that Anti-pattern word was excessive but I see so many catastrophic performances in my audits where developers use Include method.
I presented you a very « special » way to realize Include. If you use Include ONLY on the one side, performance difference is not huge and my code is really longer and more complex. But I consider that my solution would become very interesting if this code could be generated.
In a future post, I will share with you a T4 that do the job.
Note that if my DB was not local but far (SQL Azure for example), difference would be really better for my solution. Indeed, bytes received from server are really much important using Include method because you will probably have duplicated data if you have more than one row.
The main idea of these posts was not to give you a better way to write Include but to help you to realize what happens when you use Include method and what can we do if performance is something very important on your project.
Just note that, as I already mentioned, with my code, you will lose the IQueryable.
Note also that Include method is perfect for table splitting.
You also have to take in consideration that, because I don’t use DB lock, I can have incoherence with my way contrary to Include method. For example, if you first get Customers and the someone adds a new OnlineSale with a new Customer, and then you get the OnlineSales, you will get an OnlineSale without its Customer.