Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework startup time

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?

like image 969
nyhjem Avatar asked Jun 22 '12 13:06

nyhjem


2 Answers

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.

like image 115
Merenzo Avatar answered Oct 04 '22 20:10

Merenzo


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);
        }
like image 27
Henrique Miranda Avatar answered Oct 04 '22 20:10

Henrique Miranda