Entity Framework startup time
Asked Answered
D

2

11

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?

Deyoung answered 22/6, 2012 at 13:39 Comment(4)
Don't think it's possible, but I may be wrong. Look at this question to understand what's happening during first query #5634838Ludmilla
@RaphaëlAlthaus You might be right :/ It's just weird.. You can construct your own model by using the DbModelBuilder class and then calling the Build method on that object, so it should be possible to do this at compiletime instead of runtime.. But maybe it's just not implemented.Deyoung
It may take some time to check database schema compatibility. You could check this by using SQL profiler.Fulguration
This is the reason we switched back to EDMX. Though it is not very fast but certainly faster then code first. Code first is also checking database structure to perform migration etc. Plain EDMX starts to execute query immediately.Roddy
C
5

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.

Caracaraballo answered 22/6, 2012 at 13:47 Comment(1)
I've already tried that, but now I followed the steps in the article you linked to. The view is getting generated, but that first access is still really slow. I've made a little test. First I access the database with an ordenary SqlConnection and SqlCommand. It takes 0.2 sec. Just after that I use EF and do something similar (con.Users.Count();). This takes 3.08 sec..! Then I run the exact same line of code again, and from now on it only takes 0.007 sec.. So clearly EF is doing something other than generating views when you access the database for the first time..Deyoung
S
5

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);
        }
Selaginella answered 28/2, 2014 at 12:26 Comment(1)
force=true will, well, force reinitialization even if was already initialized its infra structure/internals before, so don't use force:true unless you've changed connection string or table structures.Palacio

© 2022 - 2024 — McMap. All rights reserved.