T4 Template for First Code EntityFramework 6.1 Workflow

I enjoy customizing the T4 templates used by Entity Framework 6.1 in a Code First workflow.

I have two projects in my MVC application, one for Data Access (DAL) and one for Domain Entities (Model). I am using the Data Basics from Database Data Model Wizard in a DAL project to create context and POCOs.

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

If this is not possible, I want to customize the T4 templates so that I can store the domain objects in a separate folder.

The second thing that surprises me is how can I rebuild my models if the DB changes me? Do I need to add a new model every time?

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

Thank you for your help!

Sam

+3


source to share


3 answers


I chose a non-OOB solution, relying on the EntityFramework Reverse POCO Generator extension . "This is IMHO much better than the OOB wizard, which sucks in and out of the powertools beta.

Thanks for all the opinions and agreeing with me that the OOB master needs a rethinking MS.



Sam

+1


source


Microsoft has written it so that you have to generate a new model every time. It is a pain. As a result, we have a project for generation, from which we copy the files to the final projects with the appropriate folder structure. We also generate multiple contexts from the same database.



It takes a while to get used to the T4 patterns, but after some practice they are pretty straight forward.

0


source


About modifying the model, since this is the first approach to code, it means your models are in control of everything. This way you can modify your models as many times as you want, just use the following command in the Package Manager Console to generate the code that will be modified accordingly in the database.

Add-Migration <name-goes-here>

      

And to execute your changes use

Update-Database -Verbose

      

To keep your models separate from the data access layer, you may need to provide a connection string (which points to the correct folder in your project):

This might help you. Entity Framework 6 (EF6) code initial migrations with models in a separate project

0


source







All Articles