I am creating my own custom T4 Template that integrates with an ADO.NET Entity Framework Model (.edmx file) included in my MVC Web Application.
For Reference
Please take a brief look at the following two URLs.
- Scott Hanselman - "T4 Code Generation Visual Studio Best Kept Secret"
- Visual Web Developer Team Blog - Quick Start Guide for ASP.NET MVC Developers
Short Description of What I am trying to Achieve
Use the T4 engine to generate the MVC Controller class with Action methods based on the ADO.NET Entity Framework Model's Primary Key(s).
What I Have Right Now
- MVC T4 Template files (e.g., Controller.tt, Create.tt, etc...) have been included as part of my MVC Web Project.
- I have an ADO.NET Entity Framework MyModel.edmx file in the "Models" folder.
Based on the Controller name (e.g. "ProductController"), I want to retrieve the [System.Type] information of the "Product" class from the ADO.NET Entity Framework model. I want to be able to retrieve System.Type information the same way as the MVC View T4 files (e.g. Edit.tt) as below.
MvcTextTemplateHost mvcHost = (MvcTextTemplateHost) (Host);
Type type = mvcHost.ViewDataType;
Final Goal
I want to create the Controller method code-generation to read Primary Key information and etc from the ADO.NET Entity Framework Class via Reflection
and
generate basic CRUD operations and method signatures for EDIT, DETAILS, ADD operations etc...
Where I am Stuck
However, as you can see from Quick Start Guide for ASP.NET MVC Developers article, I cannot retrieve [System.Type] for Controller T4 Template because the MvcTextTemplateHost class exposes only ViewDataType property for creating MVC Views.
My attempt to retrieve the [System.Type] by the following technique is NOT working because the modelType is being returned as null meaning it can't find the type.
Type modelType = Type.GetType(modelFullyQualifiedName, false, true);
I am assuming that this happens because the Entity Framework Model is included as part of my MVC Web Project and not included as part of a compiled .DLL library assembly.
Some Things that Will Help Me Find the Solution
- Where do I find the Source Code for the MvcTextTemplateHost class? If I can find at least the DLL file, I can probably look at how the code loads the Type information entered in the Visual Studio "Add View" dialog window.
- Is there a way to dynamically retrieve System.Type information of a Class included in the Visual Studio project from my T4 Template through the Visual Studio IDE API?
I would really appreciate if anyone can help me on this topic as this will allow me to generate 75% of the code for MVC Controller Action Methods for ADD,EDIT,DETAILS, etc.. and basic CRUD operation code.