Pre-generating views to improve query performance in Entity Framework

If you have used .NET Entity Framework, there’s a chance you will have run into performance issues when first loading your application.  One way to reduce this overhead is by pre-generating your view metadata as per this Microsoft guide.

I had a few problems using this resource so have prepared this step-by-step guide on how to pre-generate your views in EF 4.0.

Step 1: Change processing type for your .edmx model

Locate your model and open it in the designer.  Select “Copy to Output Directory” for the “Metadata Artefact Processing” option. This will result in .ssdl, .csdl and .msl files appearing in your output directory.  During debug, this will be bin/debug.

Step 2: Set up a pre-build event

Set up a pre-build event in your project by right clicking the project name, clicking properties and navigating to Build Events.  In the “Pre Build” box, include the following all on one line, replacing DataModel with the name of your model.

"%windir%Microsoft.NETFrameworkv4.0.30319EdmGen.exe" /mode:ViewGeneration "/inssdl:$(TargetDir)DataModel.ssdl" "/incsdl:$(TargetDir)DataModel.csdl" "/inmsl:$(TargetDir)DataModel.msl" "/outviews:$(ProjectDir)DataModel.Views.cs" /nologo /language:CSharp 

Step 3: Set up post-build event

Next, build your project and you’ll see three files in your output directory (bin/debug).  We can add these to the project by creating a post-build event and using XCOPY to copy these to the root of your project.

XCOPY "$(TargetDir)DataModel.csdl" "$(ProjectDir)" /R /Y XCOPY "$(TargetDir)DataModel.ssdl" "$(ProjectDir)" /R /Y XCOPY "$(TargetDir)DataModel.msl" "$(ProjectDir)" /R /Y

Step 4: Include files in your project

Build the project which contains your model and you should see the following files in root directory of your project

  • DataModel.Views.cs
  • DataModel.csdl
  • DataModel.ssdl
  • DataModel.msl

Include all these files in your project via Show All Files > Right Click > Include In Project

Step 5: Set as embedded resources

Include DataModel.csl, DataModel.ssdl, DataModel.msl as an embedded resourcees in your project by clicking on the file and selecting “Embedded Resource” as your build action in the properties window.

Step 6: Update your web.config/app.config connection string

Update your connection string and you’re ready to go. Mine looks like this.  Be sure to use fully qualified names for your resources.

<add name="projectEntities" connectionString="metadata=res://*/Project.EF.DataModel.csdl|res://*/ Project.EF.DataModel.ssdl|res://*/ Project.EF.DataModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data;Initial Catalog=xxx;Persist Security Info=True;User ID=xxx;Password=xxx;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient"/>

If you receive, “Unable to load the specified metadata resource”, the path to your model in your configuration file is probably wrong.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.