How to use a T4 template for View Generation

Background

To set the stage for this article, do take a look at Exploring the Performance of the ADO.NET Entity Framework - Part 1 to learn more about pre-generating views and how they can reduce the startup time for applications that consume ADO.NET Entity Data Models.

A while back, Jaroslaw Kowalski from our team posted a great reply about using EdmGen.exe to generate views for your ADO.NET Entity Data Model in this forum question. This information is also available on MSDN for reference.

While using EdmGen.exe is a good approach for certain view generation scenarios, it is somewhat problematic in the following situations when you use it inside Visual Studio:

1. It does not work with EDMX files (you need to give it the individual CSDL, MSL and SSDL files)

2. Getting at the individual CSDL, MSL and SSDL files is a challenge when you’ve set “Metadata Artifact Processing” to “Embed in Output Assembly” in the Entity Designer.

3. Users are forced to fuss with post-build steps in the project

This article presents an easier and more intuitive approach to generate pre-compiled views with the Text Template Transformation Toolkit (a.k.a. T4 templates) in Visual Studio 2008. If you are not familiar with T4 templates, this video provides a great introduction.

T4 template for view generation

Introduction

This article is accompanied by two T4 templates: CSharp.Views.tt for use in C# projects and VisualBasic.Views.tt for use in VB projects. Both versions do the same thing:

1. Figure out which EDMX file to process based on the file name

2. Extract the individual CSDL, MSL and SSDL from the EDMX file and

3. Use public APIs in the ADO.NET Entity Framework to generate views.

To keep things simple, the code in the T4 template follows a simple file naming convention to determine the EDMX file to process. It assumes that [edmx-file-name].Views.tt will process and generate views for the file [edmx-file-name].edmx. The views are generated in the code behind file [edmx-file-name].Views.cs or [edmx-file-name].Views.vb as the case may be.

Usage

I think this is one of those things that’s best demonstrated by actually using it, so I’ll jump right into it.

C# projects

Scenario: Generate pre-compiled views for an EDMX file in a C# project

1. Create a new C# Console project

2. Add a new ADO.NET Entity Data Model to the project via “Add…New…Item

3. Go with the default file name of Model1.edmx

4. Generate a model from a database (say, Northwind) and complete the wizard

5. Use “Add…Existing…Item” to add the C# version of the T4 template (CSharp.Views.tt) into the project in the same project directory as Model1.edmx

6. In Solution Explorer, rename the file CSharp.Views.tt to Model1.Views.tt

7. Click OK to continue if Visual Studio displays the following dialog:

clip_image002

8. In Solution Explorer, right-click Model1.Views.tt and choose "Run Custom Tool" to generate the views

9. Visual Studio runs the text transformation code in Model1.Views.tt which processes Model1.edmx to produce the generated views in the code behind file Model1.Views.cs

10. The generated file Model1.Views.cs is compiled into the output assembly when you build the project

11. If needed, click “Show All Files” in Solution Explorer to see the generated code behind file

VB projects

Scenario: Generate pre-compiled views for an EDMX file in a VB project

1. Create a new VB Console project

2. Add a new ADO.NET Entity Data Model to the project via “Add…New…Item

3. Go with the default file name of Model1.edmx

4. Generate a model from a database (say, Northwind) and complete the wizard

5. Use “Add…Existing…Item” to add the VB version of the T4 template (VisualBasic.Views.tt) into the project in the same project directory as Model1.edmx

6. In Solution Explorer, rename the file VisualBasic.Views.tt to Model1.Views.tt

7. Click OK to continue if Visual Studio displays the following dialog:

clip_image002[1]

8. In Solution Explorer, right-click Model1.Views.tt and choose "Run Custom Tool" to generate the views

9. Visual Studio runs the text transformation code in Model1.Views.tt which processes Model1.edmx to produce the generated views in the code behind file Model1.Views.vb

10. The generated file Model1.Views.vb is compiled into the output assembly when you build the project

11. If needed, click “Show All Files” in Solution Explorer to see the generated code behind file

Tips

1. As and when you make changes to the EDMX file, simply right-click Model1.Views.tt in in Solution Explorer and choose "Run Custom Tool" to generate the views

2. If you have multiple EDMX files in your project then make as many copies of the.tt file and rename appropriately to pair each with each EDMX file

3. To generate views for all EDMX files in the solution, click the “Transform All Templates” button in the Solution Explorer toolbar (the rightmost button in the toolbar)

I hope this article helps get your creative juices flowing for similar scenarios. For example, one could imagine a similar approach to do custom code generation from an EDMX file (shameless plug for a future blog post!)

Sanjay Nagamangalam
Lead PM – ADO.NET Entity Designer

Templates.zip