views:

859

answers:

3

Is there a way to take over the Entity Framework class builder? I want to be able to have my own class builder so i can make some properties to call other methods upon materialization or make the entity classes partial.

+3  A: 

Actualy they are already partials. See MSDN

Artem Tikhomirov
A: 

I'll add that not only can you can tack on your own class partial classes to those emitted by the Entity Framework, but you can also write out all the partial methods created by the generated code.

IE. There'll be a lot of partial method code which get called but the method itself in the generated code isn't really implemented. For that you can write a partial method to catch events when properties change and such. It's not a terrible way to handle some business rules.

Tyler
+1  A: 

System.Data.Entity.Design.EntityClassGenerator, is the type used in VS to generate the object layer from your .edmx file, and it is the type used by EdmGen.exe to generate the object layer from a .csdl file. Below I listed the 3 ways that you can affect the generated code. The 3rd option requires that you call EntityClassGenerator yourself. You can get your code to run automatically in VS sort of like an SingleFileGenerator by using this technique presented by Sanjay.

  1. Add code to the types through partial classes
  2. Add code to the partial methods that are called by the generated classes
  3. Hook the code generation events to inject code directly into the properties and types as they are generated. See Danny's blog post for an example