How to: Pre-Generate Views to Improve Query Performance

Before the Entity Framework can execute a query against a conceptual model or save changes to the data source, it must generate a set of local query views to access the database. The views are part of the metadata which is cached per application domain. If you create multiple object context instances in the same application domain, they will reuse views from the cached metadata rather than regenerating them. 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 (Entity Framework).

In addition to generating and validating Entity Framework model and mapping 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.

You could also use the Text Template Transformation Toolkit to generate pre-compiled views. For more information, see How to use a T4 template for View Generation.

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 that are used by an ASP.NET Web site, create the .edmx file in a separate class library, use the procedures in How to: Use a Model Defined in a Class Library for the class library project, and reference the class library project in your ASP.NET Web site project. 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 run time to ensure that they are consistent with the current version of the model and mapping files.

The procedures in this topic use the School model. You can generate this model by completing the Quickstart. You can skip the first procedure if the build process is already generating model and mapping files in the output directory.

To generate model and mapping files for the School model in the output directory

  1. In Solution Explorer, double-click the School.edmx file.

    This displays the School model in the Entity Designer.

  2. 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.

  3. Build the solution.

    This generates the model and mapping files in the output directory.

To add view generation to a Visual Basic project

  1. In Solution Explorer, select the project for which you want to specify the build event.

  2. On the Project menu, click ProjectProperties.

  3. In the Properties page, click the Compile tab.

  4. Click the Build Events button.

  5. In the Build Events dialog box, add the following pre-build event, without line breaks:

    "%windir%\Microsoft.NET\Framework\v4.0.30319\EdmGen.exe" /nologo /language:VB 
    /mode:ViewGeneration "/inssdl:$(TargetDir)School.ssdl" 
    "/incsdl:$(TargetDir)School.csdl" "/inmsl:$(TargetDir)School.msl" 
    "/outviews:$(ProjectDir)School.Views.vb"
    
  6. Click OK.

  7. Close the Project Properties page.

  8. Build the solution.

    This generates the view file School.Views.vb.

  9. In Solution Explorer, right-click the project name and select Add Existing Item.

  10. In the Add Existing Item dialog box, navigate to the project's root folder and select the School.Views.vb file.

  11. Click Add.

  12. Build the solution.

To add view generation to a C# project

  1. In Solution Explorer, select the project for which you want to specify the build event.

  2. On the Project menu, click Properties.

  3. Select the Build Events tab.

  4. In the Pre-build Event Command Line window, add the following pre-build event, without line breaks:

    "%windir%\Microsoft.NET\Framework\v4.0.30319\EdmGen.exe" /nologo /language:CSharp 
    /mode:ViewGeneration "/inssdl:$(TargetDir)School.ssdl" 
    "/incsdl:$(TargetDir)School.csdl" "/inmsl:$(TargetDir)School.msl" 
    "/outviews:$(ProjectDir)School.Views.cs"
    
  5. Build the solution.

    This generates the view file School.Views.cs.

  6. In Solution Explorer, right-click the project name and select Add Existing Item.

    The Add Existing Item dialog box appears.

  7. Navigate to the project's root folder and select the School.Views.cs file.

  8. Click Add.

  9. Build the solution.

To re-add mapping and model files as embedded resources for ASP.NET projects

  1. On the Project menu, click Add Existing Item.

  2. Browse to the output directory for the project, select School.csdl, and then click OK.

  3. In Solution Explorer, select the added file.

  4. In Properties, set Build Action to Embedded Resource.

  5. Repeat steps 1 through 3 for the School.ssdl file and the School.msl file.

  6. 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.

See Also

Reference

EDM Generator (EdmGen.exe)

Other Resources

Entity Data Model Tools
Isolating Performance with Precompiled/Pre-generated Views in the Entity Framework 4