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

Comments

  • Anonymous
    June 20, 2008
    PingBack from http://wordnew.acne-reveiw.info/?p=973

  • Anonymous
    June 22, 2008
    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:DevRAMBillingWebDataAccessRAMBilling.DALRepositoryRAMBillingEDM.Views.tt 105

  • Anonymous
    October 14, 2008
    Rob beat me to it . Blogging about T4 (the Text Template Transformation Toolkit ) had been on my list

  • Anonymous
    April 28, 2009
    As Jean Marc and Oleg pointed out T4 is getting more attention now. Tangible Engineering did now post

  • Anonymous
    June 04, 2009
    I came across several blog posts about T4 code generation you may find useful. Sanjay Nagamangalam posted

  • Anonymous
    July 18, 2009
    The comment has been removed

  • Anonymous
    July 23, 2009
    I am having the same problem as Thaabiet. I am getting the same error, please any help would be appreciated

  • Anonymous
    July 23, 2009
    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

  • Anonymous
    October 23, 2009
    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.

  • Anonymous
    January 20, 2010
    The comment has been removed

  • Anonymous
    March 09, 2010
    Same issue like Muthuraj. Please clarify how to use the created class. Greetings Andy

  • Anonymous
    April 22, 2010
    Is there instructions on how to do this for VS.net 2010 given that I'm already using one of the T4 templates? I would love to be able to add this to the T4 that's there so that I don't have to have precompile commands etc.

  • Anonymous
    June 28, 2010
    The comment has been removed

  • Anonymous
    July 15, 2010
    This T4 template works great in Visual Studio 2008 with EF 3.5, but consistently fails for me when I attempt to use it in Visual Studio 2010 with EF 4. Would it be possible to update the download to provide a working version for Visual Studio 2010/Entity Framework 4 ?

  • Anonymous
    July 16, 2010
    Same question as Brian Kuhn. Would it be possible to update the download to provide a working version for Visual Studio 2010/Entity Framework 4 ? THANKS!

  • Anonymous
    September 15, 2010
    The comment has been removed

  • Anonymous
    November 03, 2010
    Why the view file generated always has a 1 appended like MyModel.Views1.cs?

  • Anonymous
    January 04, 2011
    Do these templates still work in .NET 4.0?  Visual Studio 2010?

  • Anonymous
    January 10, 2011
    @Terry yes.  Just ran against VS2010 .NET 4.0, my ViewCount = 454! Works great. @spboyer

  • Anonymous
    March 08, 2011
    Can we download and use the template freely without causing any license issue?

  • Anonymous
    March 15, 2011
    When used in an assembly which is then sent to sgen.exe to generate a XmlSerializer assembly, this error occurs in the generated class' instance constructor : "The property or indexer 'System.Data.Mapping.EntityViewContainer.ViewCount' cannot be used in this context because the set accessor is inaccessible"

  • Anonymous
    March 15, 2011
    Setting the generated class' visibility to internal is a workaround to hide the class from sgen.exe. An option for EntityViewGenerator to control the class' visibility would be even better.

  • Anonymous
    May 20, 2011
    Startup still seems to be slow.  What can I look at to see if the generated views are actually being used?

  • Anonymous
    August 21, 2011
    Is there a way to "run custom tool" from a pre-build event or change the tt to work like other EF tt files where every time you save the edmx file it automatically re-runs this template?

  • Anonymous
    October 11, 2011
    Same question as Jason, how can we change the template to work like other EF templates where it is re-run whenever the EDMX is saved?

  • Anonymous
    September 24, 2013
    If you are getting "Argument 'xmlReaders' is not valid" exception, check out my modified version that works with .NET 4.5: blog.angeloflogic.com/.../entity-framework-t4-view-generator-and.html BTW, I find it surprising that nobody complained about compatibility with .NET 4.5 yet. Perhaps there's a better way to do it in .NET 4.5?

  • Anonymous
    October 08, 2013
    I created a C# and a VB.Net version of the template that works for both EF4 and EF5 and posted it on Visual Studio Gallery. More details here: blog.3d-logic.com/.../t4-templates-for-generating-views-for-ef4ef5-databasemodel-first-apps