Welcome to MSDN Blogs Sign in | Join | Help

system.data.objects dev guy

Ramblings about ADO.Net, the Entity Framework, and other random things from a dev guy.
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

Posted: Monday, October 27, 2008 10:31 PM by dsimmons@microsoft.com
Filed under:

Comments

Artem Govorov said:

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).

# October 29, 2008 12:29 AM

dsimmons@microsoft.com said:

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

# October 31, 2008 3:41 PM

mli said:

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.

# November 10, 2008 4:08 AM

dsimmons@microsoft.com said:

@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

# November 10, 2008 8:09 PM

David Ebbo's blog said:

T4 templates have been a pretty popular topic lately.  If you have no idea what they are, don’t

# November 26, 2008 10:18 PM

system.data.objects dev guy said:

Part of the Entity Framework FAQ . 2. Architecture and Patterns 2.1. Does Entity Framework have support

# January 8, 2009 11:48 AM

Community Blogs said:

1. Introduction This is the first article of my series about deep dive into ADO.NET Data Services. In

# March 19, 2009 4:24 PM

Web开发技术 said:

In my MSDN Magazine article on SOA Data Access I recommend exposing Data Transfer Objects (DTOs) from

# May 26, 2009 5:52 AM

VS2010学习 said:

In my MSDN Magazine article on SOA Data Access I recommend exposing Data Transfer Objects (DTOs) from

# June 21, 2009 2:45 PM
Anonymous comments are disabled
Page view tracker