T4 template for Code First Workflow of EntityFramework 6.1

entity-framework entity-framework-6


I like to customize the T4 templates used by Entity Framework 6.1 in Code First workflow.

I have 2 projects in my MVC app, one for Data Access (DAL) and other for Domain Entities (Model). I am using "Code First from database" Entity Data Model wizard in DAL project to generate context and POCOs.

However, I want to customize the T4 templates so that it generates data objects in Model project and let DbContext sit in DAL project (Separation of Concerns). So I have installed EntityFramework.CodeTemplates.CSharp NuGet package (http://msdn.microsoft.com/en-US/data/dn753860).

If it's not doable, I want to customize T4 templates so that I can store domain objects in separate folder.

Second thing which surprises me is how can I re-engineer my models if my DB changes? Do I need to add a new model each time?

I am having difficulty in customizing these T4 templates used by EF in CodeFirst due to lack of documentation.

Thanks for your help!


1/8/2015 4:23:25 PM

Accepted Answer

I opted for non-OOB solution by relying on "EntityFramework Reverse POCO Generator" extension. This IMHO is much better than OOB wizard which sucks and beta version of power tools.

Thanks for all the opinions and agreeing with me that the OOB wizard needs re-thinking by MS.


1/15/2015 2:54:04 PM

Popular Answer

About the change in model, since it is code first approach, that means your models are driving the whole thing. So you can change your models as many times as you want, just use the following command in package manager console, to generate code that will change the database accordingly

Add-Migration <name-goes-here>

And to execute your changes use

Update-Database -Verbose

To have your models seperate from data access layer, you may have to specify a connection string (which points to the correct folder in your project):

This may help you. Entity Framework 6 (EF6) code first migrations with models in separate project

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow