Share via


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

  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 Project Properties.

  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\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"
    
  6. Click OK.

  7. Close the Project Properties page.

  8. Build the solution.

    This generates the view file School.Views.cs.

  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\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"
    
  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 (Entity Framework).

See Also

Reference

EDM Generator (EdmGen.exe)

Other Resources

Entity Data Model Tools