Entity Framework initialization is SLOW -- what can I do to bootstrap it faster?

In pre EF6 view generation is known to be slow for bigger models. For now the solution is to use pregenerated views. This way you generate views at design time and are avoiding this work at runtime. To do that download EF power tools and select "Optimize Entity Data Model". It will add a C# file to your project that contains views. The down side is that you will need to do it each time your model changes. Note: to generate views with the tool it will take about the same amount of time it takes to generate views at runtime (so sometimes you need to be patient). Here is a post about EF Power Tools that might be helpful: http://blogs.msdn.com/b/adonet/archive/2011/05/18/ef-power-tools-ctp1-released.aspx

Edit

Recently I created a different solution that is much more convenient to use (note it only works on EF6) - http://blog.3d-logic.com/2013/12/14/using-pre-generated-views-without-having-to-pre-generate-views-ef6/


Here is another way to do it. It requires a bit manual work but can actually be more suited to your scenario where you would like to use MsBuild. Instead of creating views with Power Tools (I am sorry to hear that they did not work for you) you can create them manually - here are the steps:

  • First you need to get artifacts for your context. You need all - csdl, ssdl and msl files. You can use EdmxWriter to get these. Note that EdmxWriter returns an edmx file that combines all three files so you need to split them. Here is the code for this step (note that namespaces are specific to EF4, if you are thinking about using EF5 and .NET Framework 4.5 you will need to change them accordingly or select elements only by local name and not the fully qualified name):

    var ms = new MemoryStream();
    using (var writer = XmlWriter.Create(ms))
    {
        EdmxWriter.WriteEdmx(new Context(), writer);
    }

    ms.Position = 0;

    var xDoc = XDocument.Load(ms);

    var ssdl = xDoc.Descendants("{http://schemas.microsoft.com/ado/2009/02/edm/ssdl}Schema").Single();
    var csdl = xDoc.Descendants("{http://schemas.microsoft.com/ado/2008/09/edm}Schema").Single();
    var msl = xDoc.Descendants("{http://schemas.microsoft.com/ado/2008/09/mapping/cs}Mapping").Single();

    ssdl.Save("Context.ssdl");
    csdl.Save("Context.csdl");
    msl.Save("Context.msl");
  • When you have artifacts you can generate views using EdmGen tool. Since here we do it manually you need to do it from the VS Command prompt. Here is the command you use to generate views:
EdmGen /mode:ViewGeneration /incsdl:Context.csdl  /inmsl:Context.msl /inssdl:Context.ssdl /outviews:Context.Views.cs
  • Add the generated file to your project.

If you want to integrate view generation with your build system there is one more interesting option - using a T4 template. The template would take care of the above steps. You can find more details about this approach here http://blogs.msdn.com/b/adonet/archive/2008/06/20/how-to-use-a-t4-template-for-view-generation.aspx. The only problem is that the example is not for CodeFirst approach so it needs to be changed a little bit which should not be hard.

I actually created T4 templates for Code First. You can find a link to download in my blog post: http://blog.3d-logic.com/2012/05/28/entity-framework-code-first-and-pre-generated-views/

The templates are now available on Visual Studio Code Gallery. Here is the link to the post with all the details: http://blog.3d-logic.com/2012/06/13/entity-framework-codefirst-view-generation-templates-on-visual-studio-code-gallery/


View generation is in fact quite fast in the current version of Entity Framework. (6.1) There is another, broader caching solution in preparation: https://entityframework.codeplex.com/workitem/1876. You can wait for this patch to be accepted, or, if you are brave enough, you can apply it for yourself.