Sam
Sam

Reputation: 559

T4 template for Code First Workflow of EntityFramework 6.1

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!

Sam

Upvotes: 3

Views: 1105

Answers (3)

Sam
Sam

Reputation: 559

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.

Sam

Upvotes: 1

ATHER
ATHER

Reputation: 3384

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

Upvotes: 0

Andrey Ershov
Andrey Ershov

Reputation: 1803

Microsoft has written it so, that you have to generate a new model every time. It's a pain. We ended up on having a project for generation from wich we copy files to final projects with appropriate folder structure. We also generate several contexts from one database.

It takes time to get used to T4 templates, but after some practice they are quite straight-forward.

Upvotes: 0

Related Questions