Pre-generating Views to Improve Performance in EF5

Tags: Entity Framework, EF5, T4

The other day, the EF Team posted guidance on how to improve performance for Entity Framework 5.0. Their very first recommendation was to pre-generate the Views that EF uses to run its queries. Typically these views are automatically generated the first time the application is executed, but this can dramatically increase the execution time on the first query.

Their solution was to pre-generate the code using EdmGen.exe, and in 2008 the team provided a T4 template to automate that process for Database First and Model First scenarios. However, the latest guidance provided incomplete information on how to update that template to run in Visual Studio 2011. Since I'm getting ready to release several other tools for EF5, I thought it would be a good time to learn the ins and outs of publishing a template to the Visual Studio Gallery.

Using the latest DbContext genrators Microsoft put out as a guide, I was able to figure out how to have the template show up as a Code Generation Item in the Entity Designer. Just right-click anywhere in the designer, select "Add Code Generation Item", and then select "EF 5.x Views Generator". The views will automatically be added to your project, all you need to do is hit "Compile", and watch your first query execute just as quickly as all the others.

Though I will not be providing any "official" support, if you have any issues, please let me know. Oh, and you can download the template here, or by searching for "views generation" in the Visual Studio 11 Extension Manager.
Add a Comment