LINQ to SQL T4 template reloaded

July 23rd 2008 • .NET (, ) • 11,548 views • 14 responses

A newer version of this LINQ to SQL template is available.

The topic of modifying the code generation phase of LINQ to SQL comes up quite often and the limited T4 template I published here last month was good at showing the potential but wasn’t a practical replacement for the code generation phase.

I am please to make available the next version, which now…

  • Runs from the DBML therefore keeping the designer in the loop
  • Generate all the attributes for columns and tables including UpdateCheck, IsDbGenerated etc.
  • Supports associations including those with a foreign key
  • Generates appropriate attributes and code for both serialization modes

In short it generates code that is now functionally equivalent to SQL Metal with the following caveats:

  • C# only – VB.NET can be added if there is some interest
  • Stored procedures – not yet supported
  • Table inheritance – incomplete
  • DBML changes require you open and re-save the T4 template so it regenerates the code
  • Unidirectional serialization requires you add System.Runtime.Serialization to your project references (thanks Roger!)

To use the template :

  • Extract the archive and add the two files to your project
  • Right-click on the L2ST4.tt file, choose Properties and set the Custom Tool to blank
  • Rename DataClasses1.tt to the same name as your DBML file (but keeping the .tt extension) and open it
  • Click save and watch a freshly generate C# DataContext pop out
  • Switch off the LINQ to SQL designer generated C# by either setting the Custom Tool on the DBML to blank or setting the Build Action on the generated C# to None.

L2ST4.tt contains a lightweight wrapper around the DBML which is processed using LINQ to XML making the template easier to work with and providing a central for naming rules etc.

This code should be treated as a sample and hasn’t received much testing yet so feel free to leave comments or feedback here.

Some places you could take this template:

  • Generate an interface for your data context to improve mocking
  • Alternative naming and defaults
  • Splitting output into multiple files
  • New languages
[)amien

Related content

14 responses  

  1. Pingback Oleg Sych - » Recent blog posts about T4 on July 30th, 2008

    [...] Guard made several improvements in his LINQ to SQL template, including code generation based on .dbml file instead of SQL database, associations based on [...]

  2. Nikhil Kothari on August 7th, 2008

    I wonder if your code generator could be packaged into a VS code generator, and then someone can simply replace the default DBML code generator?

  3. Damien Guard on August 7th, 2008

    I wouldn’t know where to start with that but if you’re offering to show me the way ;-)

    [)amien

  4. Pingback Dew Drop - August 8, 2008 | Alvin Ashcraft's Morning Dew on August 8th, 2008

    [...] LINQ to SQL T4 Template Reloaded (Damien Guard) [...]

  5. Jon Kruger on August 10th, 2008

    I’ve always wished that I could modify the L2S code generator. Looking forward to checking it out.

  6. Rybolt on August 26th, 2008

    Thanks a lot for this template and t4 intro. I’ve been researching ways to customize LINQ to SQL generated code for a few days now. Ideally a good C# parser and CodeDOM could have been useful, but there is no good C# parser, at least an updated one.

    It seems unfortunate that we can’t use what sqlmetal gives us, and then use the template to alter it. Essentially having to recreate all that functionality to gain back control :)

  7. Damien Guard on August 26th, 2008

    I would love to see the .NET Framework include parsers for C# and VB.NET although they would probably create a CodeDOM representation which is a very ugly and awkward to use API.

    Thankfully the code-generation phase is relatively easy as this template shows and SqlMetal can take care of the trickier SQL to DBML phase.

    [)amien

  8. Spencer on August 26th, 2008

    I’m new to DSL, but when I try this out (I’m planning on making some modifications to your template), it fails because some of my dbml markup contains types that are enumerations in my project. Any suggestions or tips?

  9. Damien Guard on August 26th, 2008

    I’m not sure I get what the error is – can you post the compilation error and a short snippet of the syntax?

    [)amien

  10. Tor Christian on August 28th, 2008

    I would very much like a vb.net version of the template :)

    I am working on a case where I need to send linq objects via wcf services. I understand that after installing 3.5 sp1, wcf services supports bidirectional references. The problem is that the linq dbml generation does not support enityrefs on many:1 relationships. I was hoping that your template could solve this issue, because I need both 1:many and many:1 references :)

    http://oakleafblog.blogspot.com/2008/07/bidirectional-serialization-of-linq-to.html

  11. LINQ Junkie on December 20th, 2008

    This article shows how it’s done, but based on a different technology:
    http://www.codeproject.com/KB/linq/CustomizeLINQtoSQL.aspx
    As about VB, the site the article refers to has a VB implementation, but implementing only scenario 3 as described by the article.

  12. Pingback Silverlight Travel » ADO.NET Data Services Advanced Topics - Custom proxy based on T4 templates on February 26th, 2009

    [...] is a problem of all Microsoft ORM products, that is why community have created similar projects for LINQ to SQL (Damien Guard) and Entity Framework (Danny Simmons, ADO.NET [...]

  13. Pingback T4: Text Template Transformation Toolkit « Cosa importa se…. on July 2nd, 2009

    [...] T4 template for generating LINQ to SQL Data Context (by Damien Guard) [...]

  14. Nithin Mohan T K on March 14th, 2010

    that coolest and easiest linq2sql code generator t4 template. really thanks..

    really use ful for all of us..

Leave your response

  1. (kept private)