Filed: Code Generation, ASP.NET MVC 3 Tutorials
I have been talking a lot about the new MvcScaffolding Nuget Package as well as the new Add Controller Dialog in the ASP.NET MVC 3 Tools Update in previous posts:
These posts leverage code generation within Visual Studio 2010 whenever possible to handle a lot of the Data Access CRUD scenarios that often take place in ASP.NET MVC 3 Web Applications.
But what happens when the code generated by the MvcScaffolding Nuget Package doesn't produce code based on your conventions as an ASP.NET MVC Developer? The answer lies in customizing the T4 Templates used by MvcScaffolding.
Leveraging SQL Server Compact 4 with EF Code First in Our T4 Templates
As mentioned in a recent post, SQL Server Compact 4 Books Online, I really like SQL Compact 4. I enjoyed it first with Orchard CMS and now I have learned to enjoy it even more using the recently released EF 4.1 Code First. I would like to change the code produced by the MvcScaffolding T4 Templates such that the custom EF DbContext uses SQL Compact 4! For this to happen, I just need to specify the DefaultConnectionFactory within the DbContext as SqlCeConnectionFactory("System.Data.SqlServerCe.4.0").
In this ASP.NET MVC 3 tutorial, I will change the T4 Templates used by the MvcScaffolding Nuget Package for code generation to specify this DefaultConnectionFactory. I won't show the actual changes in the T4 Templates, since the changes won't display well in the post. My goal is to show you where the T4 Templates exist and the result of the changes so you can customize these templates for your needs!
Customizing T4 Templates used by MvcScaffolding for Code Generation
Once you install MvcScaffolding via the Package Management Console in Visual Studio 2010 there are MvcScaffolding and T4Scaffolding Folders under Packages in your Visual Studio Solution filled with T4 Templates used for code generation. In the case of my sample solution, called Scaffolding5, one can see the folders as mentioned within the Packages Directory for this Visual Studio Solution:

In the tools directory for T4 Scaffolding are the T4 Templates used for code generation and I want to change a template used to generate the custom EF DbContext Class so that it will target SQL Compact 4! There are T4 code generation templates for both C# and VB and I find the T4 Template for my solution:

Now I just need to change the T4 Template so that the Entity Framework DefaultConnectionFactory is SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"), which means the custom EF DbContext will use SQL Compact 4 instead of SQL Server Express! If ever I need to migrate from SQL Compact 4 to SQL Server I will use WebMatrix!
After changing the T4 Template and re-executing the Add Controller Dialog, my custom EF DbContext now automatically targets SQL Server Compact 4!

Conclusion
ASP.NET MVC Web Developers can customize the code generated by MvcScaffolding by modifying the T4 Templates found in the MvcScaffolding and T4Scaffolding folders under Packages. Modify these T4 Templates so that the code generation produces output using your custom code conventions!
These and other tutorials can be found in my list of ASP.NET MVC 3 Tutorials!
David Hayden