Custom changes to entity generated classes

c# entity-framework-6 poco t4


I am using VS 2013, EF 6, and database first.

I have generated entity from database and would like to shut off auto generation as I would like to add custom attributes to the generated classes. My classes were generated using T4, how do I make custom changes / disable auto generation?


3/6/2015 8:37:38 PM

Accepted Answer

  1. You could take a code-first approach (EDIT: @Steve Greene's helpful link) and write the classes yourself.

  2. If you want to keep the auto-generation, you will have to edit the .tt file itself. Editing the file is not fun, especially with no intellisense, and trying to figure out what someone else has done to it six months later when you are trying to maintain code is even less fun.

  3. Finally, you could make a tiny change to the .tt file to declare your entity classes as partial and extend those classes in files elsewhere as you choose.

3 is the solution that was taken on a past project I worked on, and I cannot say that I recommend it, as it has lead to bloated entity classes with properties that do not necessarily represent columns in the database. Furthermore, it has encouraged code that should have lived in our business layer to creep into our data layer. I am still cleaning up the mess that was left by those decisions.

2 at least keeps your classes cleaner and discourages people from changing entity classes willy-nilly, but again, someone has to be able to figure out what the heck you did to that .tt file...

1 I have less experience with, but seems to me to be the cleanest approach. You will still have to be diligent about maintaining your classes properly.

3/6/2015 8:54:07 PM

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