Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Accessing Data
ADO.NET
Programming Guide
 How to: Pre-Generate Views to Impro...
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
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.

NoteNote

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

NoteNote

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

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Two things you should notice      Tamir Shlomi ... Thomas Lee   |   Edit   |   Show History
If you will choose to leave the mapping and model files on the file system and not re-add them as embedded resource,
so after applying this change, the connection strng for the EDMX is changed, and the "metadata" property for the connection string is change to refere the specific files from the file system (the ssdl/csdl/msl) instead of their old location from the assembly resource, so you need to update your connection string at the executing assembly app.config (or the web.config).

Another thing you have to do, is to include the model.ssdl/csdl/msl into the project and set their "Copy to output folder" property from "Do not copy" into "Copy if newer" or "copy always" so when you build your project, any project that reference your EDMX dll will have them in his bin and the connection string metadata will be able to find them.
Tags What's this?: Add a tag
Flag as ContentBug
Compiling uses default namespace      mikebridge   |   Edit   |   Show History
When precompiling I discovered that the mapping files are referenced with the default namespace. I did not need to add the namespace before precompiling. This meant my metadata string went from:
res://*/MIModel.csdl|res://*/MIModel.ssdl|res://*/MIModel.msl
to:
res://*/MIEDM.MIModel.csdl|res://*/MIEDM.MIModel.ssdl|res://*/MIEDM.MIModel.msl


Tags What's this?: Add a tag
Flag as ContentBug
Need to add line to Pre-Build event to embed the msl|csdl|ssdl file      mikebridge   |   Edit   |   Show History
I think there is a problem with this description. I don't think it's possible to get the intermediate files to a place within the project where they can be embedded without adding an extra precompilation step.

The only option for generating the msl/csdl/ssdl files on the filesystem is "copy to output directory" in VS2008, so it creates the files in e.g. your TARGETDIR directory rather than somewhere within PROJECTDIR. Unless I'm mistaken, you need to have these files in the PROJECTDIR directory in order to embed them. The "To re-add mapping and model files..." suggestion above creates a shadow copy when you "Add Existing Item", NOT when you recompile. This means that the MSL|CSDL|SSDL files in the project directory are stale with every subsequent rebuild.

I think the only solution is to move the generated .csdl files in the pre-compilation step:

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

Tags What's this?: Add a tag
Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement | Site Feedback
Page view tracker