I'm wondering if it is possible to speed up the first query made with EF code first.
I've made a small test program with one entity containing 2 fields, and the first query takes 2.2 seconds, the second query (which is the exact same) takes 0.006 second.
I am already precompiling the view, so that wont help here. I think the problem is that it takes some time to contruct the model in memory, but should it take that long? And is there a way to precompile this model like there is with the views?
This article: Squash Entity Framework startup time with pre-compiled views describes a solution in detail.
It involves using the Optimize Entity Data Model option in Entity Framework Power Tools to generate a pre-compiled .Views class file.
When you make your first query, EF initializes itself and that takes some time. I don't think there's much to do in order to speed up EF's infrastructure initialization but, if what you are really looking is to speed up the first query you make and not EF's initialization itself, well, you can try to force EF to initialize before running your first query.
using (var db = new MyContext())
{
db.Database.Initialize(force: true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With