How to: Pre-Generate Views to Improve Query Performance (Entity Framework)
Before a query can be executed against an Entity Data Model (EDM), the Entity Framework generates a set of views that are used to access the database. The Entity Framework generates views the first time that a query is executed and then maintains these views for the lifetime of the ObjectContext instance. Because view generation is a significant part of the overall cost of executing a single query, the Entity Framework enables you to pre-generate these views and include them in the compiled project. For more information, see Performance Considerations for Entity Framework Applications. In addition to generating and validating EDM mapping and model files, the EDM Generator (EdmGen.exe) tool is also used to pre-generate these views. This topic shows how to use EdmGen.exe to pre-generate views for the School model and add the view file to the project. The School model is created in the Entity Framework Quickstart. The final procedure shows you how to re-add the model and mapping files as embedded resources to an ASP.NET Web application.
Note
The procedures in this topic use pre-build and post-build events in Visual Studio, which are not supported in ASP.NET Web sites. To pre-generate views for an EDM that is used by an ASP.NET Web site, you should create the EDM in a separate class library, use the following procedure for the class library project, and reference the class library project in your ASP.NET Web site project. For more information, see How to: Use a Model Defined in a Class Library. Alternatively, consider using an ASP.NET Web application project instead of an ASP.NET Web site. This enables you to include pre-generated views in the same project as the ASP.NET Web application by using the procedures in this topic.
Pre-generated views are validated at runtime to ensure that they are consistent with the current version of the EDM. This procedure ensures that the views match the EDM. You can skip the first procedure if the build process is already generating model and mapping files in the output directory. The procedures in this topic use the School model. You can generate this model by completing the Quickstart (Entity Framework).
Note
Visual Studio 2008 Service Pack 1 (SP1) is required to perform the procedures in this topic.
To generate model and mapping files for the School model in the output directory
In Solution Explorer, double-click the School.edmx file.
This displays the School model in the Entity Designer.
In the Model Browser, select the SchoolModel model and change Metadata Artifact Processing to Copy to Output Directory.
This ensures that the model and mapping files are generated in the output directory.
Build the solution.
This generates the model and mapping files in the output directory.
To add view generation to a Visual Basic project
In Solution Explorer, select the project for which you want to specify the build event.
On the Project menu, click Project Properties.
In the Properties page, click the Compile tab.
Click the Build Events button.
In the Build Events dialog box, add the following pre-build event, without line breaks:
"%windir%\Microsoft.NET\Framework\v3.5\EdmGen.exe" /nologo /language:VB /mode:ViewGeneration "/inssdl:$(TargetDir)School.ssdl" "/incsdl:$(TargetDir)School.csdl" "/inmsl:$(TargetDir)School.msl" "/outviews:$(ProjectDir)School.Views.vb"
Click OK.
Close the Project Properties page.
Build the solution.
This generates the view file School.Views.cs.
In Solution Explorer, right-click the project name and select Add Existing Item.
In the Add Existing Item dialog box, navigate to the project's root folder and select the School.Views.vb file.
Click Add.
Build the solution.
To add view generation to a C# project
In Solution Explorer, select the project for which you want to specify the build event.
On the Project menu, click Properties.
Select the Build Events tab.
In the Pre-build Event Command Line window, add the following pre-build event, without line breaks:
"%windir%\Microsoft.NET\Framework\v3.5\EdmGen.exe" /nologo /language:CSharp /mode:ViewGeneration "/inssdl:$(TargetDir)School.ssdl" "/incsdl:$(TargetDir)School.csdl" "/inmsl:$(TargetDir)School.msl" "/outviews:$(ProjectDir)School.Views.cs"
Build the solution.
This generates the view file School.Views.cs.
In Solution Explorer, right-click the project name and select Add Existing Item.
The Add Existing Item dialog box appears.
Navigate to the project's root folder and select the School.Views.cs file.
Click Add.
Build the solution.
To re-add mapping and model files as embedded resources for ASP.NET projects
On the Project menu, click Add Existing Item.
Browse to the output directory for the project, select School.csdl, and then click OK.
In Solution Explorer, select the added file.
In Properties, set Build Action to Embedded Resource.
Repeat steps 1 through 3 for the School.ssdl file and the School.msl file.
In Solution Explorer, double-click the App.config file and then modify the Metadata parameter in the connectionString attribute based on one of the following formats:
Metadata=
res://<assemblyFullName>/<resourceName>;
Metadata=
res://*/<resourceName>;
Metadata=res://*;
The
resourceName
may include the project namespace. For more information, see Connection Strings (Entity Framework).