Skip to content

LINQ to SQL templates updated, now on CodePlex  

My templates that allow you to customize the LINQ to SQL code-generation process (normally performed by SQLMetal/LINQ to SQL classes designer) have been updated once again.


  • Now licensed under the Microsoft Public License and hosted at CodePlex
  • User options specified with a var options block at the start of the template
  • Option for each class to be a separate file that is reflected in the VS project (EntityPerFile=true)
  • Detection and support of IsComposable functions
  • General code clean-up and better error handling such as missing DBML file


CodePlex makes it easier for people to be able to see and merge updates in with their own modified versions as well as report issues via the issue tracker etc. There is also an RSS feed that lets you keep track of releases, source updates or whatever else you are interested in.

For now it is a grab-the-source style release but I hope to publish downloadable tested releases wrapped up in a Visual Studio Installer (VSI) package to make getting started easier soon.  Feel free to grab the sources directly via TFS/Subversion to be able to diff them with your own modified versions.



18 responses  

  1. pingback

    […] LINQ to SQL Templates Updated, Now on CodePlex (Damien Guard) […]

    Dew Drop - January 20, 2009 | Alvin Ashcraft's Morning DewJanuary 20th, 2009
  2. pingback

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

    Experimental LINQ to SQL template » DamienGJanuary 22nd, 2009
  3. pingback

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

    LINQ to SQL T4 template reloaded » DamienGJanuary 22nd, 2009
  4. viewing plug-in with moonlight for linux. works well.

    matt – January 25th, 2009
  5. pingback

    […] Pour ceux que ça intéresse voici le lien : […]

    Personnaliser la génération du code Linq To Sql | Sébastien Alves - Ingénieur InformatiqueApril 5th, 2009
  6. Brilliant!

    I will make it to work on separate assemblies. One for the entities and one for the data access.
    This looks really great. Thanks for the effort!!

    How do you get the syntaxt highlighting on the .TT source (without VS complaining about whitespace and identifiers – I set it as a C# file)?



    Melle – June 19th, 2009
  7. @Melle you’ll need to install either the Clarius or the Tangible T4 editors as there is no syntax highlighting in-the-box.


    Damien GuardJune 19th, 2009
  8. When I try it I get…

    System.Runtime.Serialization.InvalidDataContractException: Type 'System.Runtime.Serialization.DataContractSerializer' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. See the Microsoft .NET Framework documentation for other supported types.
    at VOC3_Web_Services.VOC3_Web_Services.getTitle(String _user, String _criterion) in C:\Download\C#_Projects\VOC3_Web_Services\VOC3_Web_Services\VOC3_Web_Services.asmx.cs:line 123

    Also, how do I remove it? I took out the “L2ST4.ttinclude” “” files but now get the following Warning…

    The custom tool ‘MSLinqToSQLGenerator’ failed while processing the file ‘Data_Layer\VOC3_DB.dbml’.

    Thank you.

    Ken_S. – July 8th, 2009
  9. This is a fantastic piece of plumbing and will save my team a lot of work, specifically in the realm of serialization. I really appreciate your efforts, Damien. I really hope DataContract serialization support is native in the next version of Visual Studio LINQ to SQL.

    Jordan Rieger – October 6th, 2009
  10. Love the reorganised code but not so taken by the CA warnings I get from the code that gets generated.

    How do I surpress any code warnings from the generated code. The project is marked to ignore such but both standard VS2008 and running with this template causes CA to shout out warnings.

    Regards, Nigel

    Nigel Deakin – November 3rd, 2009
  11. @Nigel: The warnings I see are:

    1. Naming – casing/underscores/discrete (change the names in the DBML)
    2. Usage – Settable collection properties (would be a breaking change but you could suppress this warning by modifying the template)


    Damien GuardNovember 3rd, 2009
  12. Hi,
    what if I have columns like ‘private’ and ‘operator’ in my database table. Compiling the code generated with this template fails, obviously. I can’t (or lets say don’t want to) make any changes to my tables… Any ideas?

    Thanks, Pavel.

    Pavel Kubik – November 10th, 2009
  13. Great tool! It’s really nice to be able to customize the L2S code like that. I used it to fix a circular reference problem LINQ was having when serializing a parent entity to JSON (by adding [ScriptIgnore] above any Associations).

    On another note, the line base(global::.Default., mappingSource) was causing a build error for me (couldn’t find Default). Is this just because I’m using VS2010b2?


    Daniel Cotter – November 11th, 2009
  14. @Daniel : This is normally caused by a DBML coming from another project. The DBML tends to reference the connection via the app.config but the app.config wasn’t copied. Opening the DBML in the designer will detect this and remove the offending code so you can setup a new connection.


    Damien GuardNovember 11th, 2009
  15. Hi Damien,

    When using the built-in generator the namespace of my DataContext is MyCompany.Database (the .dbml is located in the folder ‘Database’ within the project).
    But with using the template, the namespace ‘Database’ is missing.
    Any ideas?


    Harro Schmidt – December 23rd, 2009
  16. Hi,

    Excuse me sir … where is the .wmv file? I’m trying to build an MetaData file automatically having as a start point my .dbml LINQ to SQL DataContext File.

    Carlos Porras (El Salvador)

    KLCA – February 19th, 2010
  17. pingback

    […] Guard has put together some T4-templates (read more) that could be used to take control of how the DataContext and the entities are generated. All I […]

    Linq to Sql – How-to Separate the entities and the DataContext. « Daniel WertheimMarch 14th, 2010
  18. pingback

    […] a developer at Microsoft and is one of the people on the original LINQ to SQL team.  He created a T4 Template that works with LINQ to SQL to offer code generation while still taking advantage of the ORM […]

    LINQ to SQL T4 Templates and Nullable Foreign Keys | Matthew SigmanNovember 9th, 2011

Respond to this