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:
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:
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
PingBack from http://wordnew.acne-reveiw.info/?p=973
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
Rob beat me to it . Blogging about T4 (the Text Template Transformation Toolkit ) had been on my list
As Jean Marc and Oleg pointed out T4 is getting more attention now. Tangible Engineering did now post
I came across several blog posts about T4 code generation you may find useful. Sanjay Nagamangalam posted
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.
I am having the same problem as Thaabiet.
I am getting the same error, please any help would be appreciated
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
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.
I get a System.OutOfMemoryException in line viewGenerator.GenerateViews(mappingItems, writer).
My DataModel has 124 Tables and 351 Relations.
Can you give me some tip to avoid this exception without reducing the DataModel size?
Same issue like Muthuraj.
Please clarify how to use the created class.
Greetings Andy
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.
thaabiet,seba, the template contain an error in msdl namespace, change this line
XNamespace mslns = "urn:schemas-microsoft-com:windows:storage:mapping:CS";
for this
XNamespace mslns = "schemas.microsoft.com/…/cs";
Unai
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 ?
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!
Brian Kuhn, Daniel:
the template contain an error in msdl namespace, change this line
XNamespace mslns = "urn:schemas-microsoft-com:windows:storage:mapping:CS";
for this
XNamespace mslns = "schemas.microsoft.com/…/cs";
Unai
Why the view file generated always has a 1 appended like MyModel.Views1.cs?
Do these templates still work in .NET 4.0? Visual Studio 2010?
@Terry yes. Just ran against VS2010 .NET 4.0, my ViewCount = 454! Works great.
@spboyer
Can we download and use the template freely without causing any license issue?
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"
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.
Startup still seems to be slow. What can I look at to see if the generated views are actually being used?
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?
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?
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?
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