How to use a T4 template for View Generation

Published 20 June 08 12:27 PM | dpblogs 

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

Attachment(s): Templates.zip

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Pregnant Man » How to use a T4 template for View Generation said on June 20, 2008 3:41 PM:

PingBack from http://wordnew.acne-reveiw.info/?p=973

# Thaabiet said on June 23, 2008 6:41 AM:

Hi. I've followed the above procedure but get the following error: Compiling transformation: Argument '1': cannot convert from 'System.Xml.XmlReader[]' to 'System.Data.Common.DbConnection' c:\Dev\RAMBillingWeb\DataAccess\RAMBilling.DAL\Repository\RAMBillingEDM.Views.tt 105

# ASPInsiders said on October 14, 2008 4:09 PM:

Rob beat me to it . Blogging about T4 (the Text Template Transformation Toolkit ) had been on my list

# Tim Fischer's .NET Small and Medium App Blog said on April 29, 2009 4:08 AM:

As Jean Marc and Oleg pointed out T4 is getting more attention now. Tangible Engineering did now post

# VS2010学习 said on June 5, 2009 7:06 AM:

I came across several blog posts about T4 code generation you may find useful. Sanjay Nagamangalam posted

# Muthuraj said on July 19, 2009 1:27 AM:

We are using ADO.NET Entity for our ASP.NET application.

I have read that the pre-generated views improves the performance. Referred to this blog post,

I generated the views. The namespace & classes generated as

namespace Edm_EntityMappingGeneratedViews

{

   /// <Summary>

   /// The type contains views for EntitySets and AssociationSets that were generated at design time.

   /// </Summary>

   public sealed class ViewsForBaseEntitySets4D4A6E0AA7AF6B2298FABB4F22235831 : System.Data.Mapping.EntityViewContainer

   {

       /// <Summary>

       /// The constructor stores the views for the extents and also the hash values generated based on the metadata and mapping closure and views

       /// </Summary>

       public ViewsForBaseEntitySets4D4A6E0AA7AF6B2298FABB4F22235831()

       {

           this.EdmEntityContainerName = "JSEntities";

I added this to my data layer and test the performance. Couldn't see much improvement. CPU usage always goes to 20-30% utilization (response timing is good) and reduce back to 0% in 500ms - 1 sec. I think the CPU utilization goes high because of view generation every time.

I couldn't understand how the entity framework knows that this is my pre-generated view class for my model eventhough the MyModel.edmx & MyModel.Views.cs matches with filename.

Should I have to update Web.Config or App.Config to map the View class to model somewhere?

Please clarify.

# Seba said on July 23, 2009 8:44 AM:

I am having the same problem as Thaabiet.

I am getting the same error, please any help would be appreciated

# Seba said on July 23, 2009 11:24 AM:

now I am facing Muthuraj issue.

I don't see any performance improvements and don't see where is set to read model.view.cs insted of edex. Any help on this thanks

# James said on October 23, 2009 7:34 PM:

Thanks for the tip.  This worked well to eliminate a 45 second startup load time in an EDMX model in which one of our tables contained 27 foreign keys to other tables.

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

Search

This Blog

Syndication

Page view tracker