Partager via


Using T4 Templates to generate EF classes

Spurred on both by the success of Damien Guard in using T4 to generate Linq to SQL code and of Jeff Reed from the EF team who built a prototype for using T4 to generate EF code, I recently hacked something up that makes it fairly easy to use T4 to generate EF classes.  This is an early draft--there are a number of "to do" items--but hopefully someone will find it useful.

In order to try it out, all you need to do is:

  1. Download the template and extract it from this zip file.
  2. Add it to your solution and give it the same name as your EDMX file (except with .tt extension rather than .edmx).
  3. Set the Build Action on the .designer.cs file to None (since you will now use the code generated by the T4 template rather than what is generated by the EDM designer).

The output now is mostly the same as what the shipping codegen creates except that:

  • I don't have a VB version yet.  Sorry VB fans.  This goes on the to-do list, and it really shouldn't be that hard.
  • I added a bunch of using statements at the top of the file so that the code throughout the file can be shorter and easier to read--this means that if you have a model with an entity type whose name conflicts with a type in one of the included namespaces, then you will get a compiler error--you could always modify the template to fully qualify things instead.
  • I added a series of #region directives so that you can nicely collapse things and navigate the generated code.
  • There are a few things that I haven't implemented yet (see to do comments at top of the template).

Already, though, it's nicer to look at, and if you would like to customize the output, this is a much more pleasant starting point than codegen events with the existing code-dom based generator.

Let me know what you think.

- Danny

Comments

  • Anonymous
    October 28, 2008
    Hi Danny, very interesting technique, I may consider using it for one of my projects to override some designer code generation issues. One possible problem function imports. I've found them in your ToDo list, but when I tried to implement them - I found that System.Data.Metadata.Edm.EntityContainer does not expose FunctionImports (it has them but as internal property).

  • Anonymous
    October 31, 2008
    Artem, Sorry for not responding sooner.  The basic answer for this will be to parse the CSDL XML file directly and get the information from there.  It's nice when we can use the metadata APIs to get the info easily, but there are ways around things when they are misisng.

  • Danny
  • Anonymous
    November 09, 2008
    Hi Danny, Very interesting approach, and for sure easier than hooking into the original code generation process. I was trying to make this .tt file dependent upon the .edmx file, as to automatically regenerate when saving the .edmx file. No success however. Can this be done you think? Thanks, Michel.

  • Anonymous
    November 10, 2008
    @Michel, There's not a real simple way to accomplish this.  The standard T4 mechanism is to just manually generate.  We're looking into some ideas to make this more automatic (but still giving control over whether or not you want it to happen all the time) in VS2010, but I don't yet have a good suggestion for you to use with VS2008sp1.  If I come up with something, I'll post it to my blog.

  • Danny
  • Anonymous
    November 26, 2008
    T4 templates have been a pretty popular topic lately.  If you have no idea what they are, don’t

  • Anonymous
    January 08, 2009
    Part of the Entity Framework FAQ . 2. Architecture and Patterns 2.1. Does Entity Framework have support

  • Anonymous
    March 19, 2009

  1. Introduction This is the first article of my series about deep dive into ADO.NET Data Services. In
  • Anonymous
    May 25, 2009
    In my MSDN Magazine article on SOA Data Access I recommend exposing Data Transfer Objects (DTOs) from

  • Anonymous
    June 21, 2009
    In my MSDN Magazine article on SOA Data Access I recommend exposing Data Transfer Objects (DTOs) from