T4 template for Code First Workflow of EntityFramework 6.1

entity-framework entity-framework-6


I enjoy modifying the Entity Framework 6.1's Code First workflow-based T4 templates.

In my MVC application, I have two projects: one for Domain Entities and the other for Data Access (DAL) (Model). To create context and POCOs, I use the "Code First from Database" Entity Data Model wizard in the DAL project.

However, I want to alter the T4 templates so that data objects are generated in the Model project while DbContext is left in the DAL project (Separation of Concerns). I have therefore installed EntityFramework. NuGet package called CodeTemplates.CSharp (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.

My second surprise is how I can re-engineer my models if my database changes. Should I always add a new model?

Due to a lack of documentation, I am finding it challenging to modify these T4 templates used by EF in CodeFirst.

I appreciate your support.


1/8/2015 4:23:25 PM

Accepted Answer

My non-OOB option was to use the "Generator for Reverse POCO in EntityFramework" extension. This, in my opinion, is far superior to the terrible OOB wizard and the beta version of power tools.

Thank you for sharing your thoughts and agreeing with me that Microsoft should reconsider the OOB wizard.


1/15/2015 2:54:04 PM

Popular Answer

Regarding the model update, since it is a code-first approach, your models are what are driving everything. Use the following command in the package management console to build code that will alter the database appropriately so that you can change your models as often as you like.

Add-Migration <name-goes-here>

and utilize to put your modifications into effect

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):

You could benefit from this. Migrations using Entity Framework 6 (EF6) code first and models in a different 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