Moving T4 Toolbox LINQ to SQL classes generator into our source code

Mar 29, 2013 at 12:13 AM
I've been looking into moving our Linq to SQL generation over to using T4 templates for some time now but have had a tough time finding a project that has been maintained and allows for at least results that are comparable with the out of the box experience offered by DBML within Visual Studio.

I've checked out the T4 Toolbox along with many of your very informative posts and just recently began looking into how to use T4 Toolbox for the purpose described above. Unfortunately we are using Visual Studio 2012 and since your pulling out support for the out of the box generator for Linq to SQL I'm following your suggestion and looking for some support for how to get some of the capabilities pushed directly into our project source code. You did mention NuGet being a better source for distributing such T4 templates but I don't see any decent ones available for Linq to SQL that are not also trying to lock users into some new custom ORM. At this point I'm wondering how much effort it would take to pull out the LINQ to SQL related parts of the templates that used to be supplied with the T4 Toolkit and place them directly into my project.

I've attempted to do this but after remapping a few files paths to point to a folder where I stuck the .tt files I get this result when trying to run my main DataContext level template DataClasses1.tt which looks like below.
<#@ template language="C#" debug="True" #>
<#@ output extension="cs" #>
<#@ include file="T4Toolbox.tt" #>
<#@ include file="LinqToSql.tt" #>
<#
    // Generate entity classes from a LINQ to SQL class model (.dbml file)
    LinqToSqlGenerator generator = new LinqToSqlGenerator();
    generator.DbmlFile = "DataClasses2.dbml"
    generator.Run();
#>
When I do this see errors like the ones listed below which makes it look like I will need somehow to get the code parts of the T4 Toolbox like TransactSqlTemplate in order to get this stuff working. Seeing that I don't want to create my own Visual Studio add-in to get this working I was wondering if you could point me in the right direction or give a yes or no answer about where what I'm attempting to do is feasible given that I would rather spend less than a couple of hours to get this working.

Error 1 Compiling transformation: The type or namespace name 'TransactSqlTemplate' could not be found (are you missing a using directive or an assembly reference?) c:\Users\me\Documents\Visual Studio 2012\Projects\LinqToSqlT4TemplateTest\LinqToSqlT4TemplateTest\LinqToSql\LinqToSqlTableBase.tt 9 52 LinqToSqlT4TemplateTest
Error 2 Compiling transformation: The type or namespace name 'Database' could not be found (are you missing a using directive or an assembly reference?) c:\Users\me\Documents\Visual Studio 2012\Projects\LinqToSqlT4TemplateTest\LinqToSqlT4TemplateTest\LinqToSql\LinqToSqlDataContextTemplate.tt 30 12 LinqToSqlT4TemplateTest
Error 3 Compiling transformation: The type or namespace name 'Connection' could not be found (are you missing a using directive or an assembly reference?) c:\Users\me\Documents\Visual Studio 2012\Projects\LinqToSqlT4TemplateTest\LinqToSqlT4TemplateTest\LinqToSql\LinqToSqlDataContextTemplate.tt 137 34 LinqToSqlT4TemplateTest
Error 4 Compiling transformation: 'Microsoft.VisualStudio.TextTemplatingDFF1CCA611C0C4496ECFC13E4C4CD837B4EE01EAECC109EF12C6697C3023A14BFC1CB5084D3FAB4B5A337AF8F9B9E92D7E1B27894AF2916A961947C6928737EE.GeneratedTextTransformation.LinqToSqlDataContextTemplate.Database' is a 'property' but is used like a 'type' c:\Users\me\Documents\Visual Studio 2012\Projects\LinqToSqlT4TemplateTest\LinqToSqlT4TemplateTest\LinqToSql\LinqToSqlDataContextTemplate.tt 166 34 LinqToSqlT4TemplateTest
Error 5 Compiling transformation: The type or namespace name 'Table' could not be found (are you missing a using directive or an assembly reference?) c:\Users\me\Documents\Visual Studio 2012\Projects\LinqToSqlT4TemplateTest\LinqToSqlT4TemplateTest\LinqToSql\LinqToSqlDataContextTemplate.tt 191 34 LinqToSqlT4TemplateTest
Error 6 Compiling transformation: The type or namespace name 'Association' could not be found (are you missing a using directive or an assembly reference?) c:\Users\me\Documents\Visual Studio 2012\Projects\LinqToSqlT4TemplateTest\LinqToSqlT4TemplateTest\LinqToSql\LinqToSqlEntityClassTemplate.tt 56 45 LinqToSqlT4TemplateTest
Error 7 Compiling transformation: The type or namespace name 'Column' could not be found (are you missing a using directive or an assembly reference?) c:\Users\me\Documents\Visual Studio 2012\Projects\LinqToSqlT4TemplateTest\LinqToSqlT4TemplateTest\LinqToSql\LinqToSqlEntityClassTemplate.tt 62 45 LinqToSqlT4TemplateTest
Error 8 Compiling transformation: 'Microsoft.VisualStudio.TextTemplatingDFF1CCA611C0C4496ECFC13E4C4CD837B4EE01EAECC109EF12C6697C3023A14BFC1CB5084D3FAB4B5A337AF8F9B9E92D7E1B27894AF2916A961947C6928737EE.GeneratedTextTransformation.LinqToSqlEntityClassTemplate.AccessModifier(Association)' is a 'method' but is used like a 'type' c:\Users\me\Documents\Visual Studio 2012\Projects\LinqToSqlT4TemplateTest\LinqToSqlT4TemplateTest\LinqToSql\LinqToSqlEntityClassTemplate.tt 1343 36 LinqToSqlT4TemplateTest
Error 9 Compiling transformation: The type or namespace name 'MemberModifier' could not be found (are you missing a using directive or an assembly reference?) c:\Users\me\Documents\Visual Studio 2012\Projects\LinqToSqlT4TemplateTest\LinqToSqlT4TemplateTest\LinqToSql\LinqToSqlEntityClassTemplate.tt 1354 36 LinqToSqlT4TemplateTest
Error 10 Compiling transformation: 'Microsoft.VisualStudio.TextTemplatingDFF1CCA611C0C4496ECFC13E4C4CD837B4EE01EAECC109EF12C6697C3023A14BFC1CB5084D3FAB4B5A337AF8F9B9E92D7E1B27894AF2916A961947C6928737EE.GeneratedTextTransformation.LinqToSqlTableBaseTemplate.Table' is a 'property' but is used like a 'type' c:\Users\me\Documents\Visual Studio 2012\Projects\LinqToSqlT4TemplateTest\LinqToSqlT4TemplateTest\LinqToSql\LinqToSqlTableBase.tt 46 40 LinqToSqlT4TemplateTest
Error 11 A namespace cannot directly contain members such as fields or methods C:\Users\me\Documents\Visual Studio 2012\Projects\LinqToSqlT4TemplateTest\LinqToSqlT4TemplateTest\DataClasses1.cs 1 1 LinqToSqlT4TemplateTest
Error 12 A namespace cannot directly contain members such as fields or methods C:\Users\me\Documents\Visual Studio 2012\Projects\LinqToSqlT4TemplateTest\LinqToSqlT4TemplateTest\LinqToSql.cs 1 1 LinqToSqlT4TemplateTest