[{"data":1,"prerenderedAt":174},["ShallowReactive",2],{"blog:2008:linq-to-sql-t4-template-reloaded":3,"blogMore-Development":160,"comments-linq-to-sql-t4-template-reloaded":173},{"id":4,"title":5,"body":6,"category":140,"commentCount":141,"date":142,"description":24,"excerpt":143,"extension":144,"filenames":145,"hidden":146,"image":145,"meta":147,"minutes":138,"navigation":148,"path":149,"seo":150,"showCategory":145,"stem":151,"tags":152,"updated":145,"url":157,"wordCount":158,"__hash__":159},"content\u002Fblog\u002F2008\u002Flinq-to-sql-t4-template-reloaded.md","LINQ to SQL T4 template reloaded",{"type":7,"value":8,"toc":136},"minimark",[9,22,25,28,44,47,71,74,100,105,113,116,130],[10,11,12],"blockquote",{},[13,14,15,16,21],"p",{},"A newer version of ",[17,18,20],"a",{"href":19},"\u002Fblog\u002F2009\u002Flinq-to-sql-templates-updated-now-on-codeplex\u002F","this LINQ to SQL template is available",".",[13,23,24],{},"The topic of modifying the code generation phase of LINQ to SQL comes up 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.",[13,26,27],{},"I am please to make available the next version, which now…",[29,30,31,35,38,41],"ul",{},[32,33,34],"li",{},"Runs from the DBML therefore keeping the designer in the loop",[32,36,37],{},"Generate all the attributes for columns and tables including UpdateCheck, IsDbGenerated etc.",[32,39,40],{},"Supports associations including those with a foreign key",[32,42,43],{},"Generates appropriate attributes and code for both serialization modes",[13,45,46],{},"In short it generates code that is now functionally equivalent to SQL Metal with the following caveats:",[29,48,49,52,55,58,61],{},[32,50,51],{},"C# only: VB.NET can be added if there is some interest",[32,53,54],{},"Stored procedures: not yet supported",[32,56,57],{},"Table inheritance: incomplete",[32,59,60],{},"DBML changes require you open and re-save the T4 template so it regenerates the code",[32,62,63,64,70],{},"Unidirectional serialization requires you add System.Runtime.Serialization to your project references (",[17,65,69],{"href":66,"rel":67},"https:\u002F\u002Foakleafblog.blogspot.com",[68],"nofollow","thanks Roger","!)",[13,72,73],{},"To use the template:",[29,75,76,79,87,94,97],{},[32,77,78],{},"Extract the archive and add the two files to your project",[32,80,81,82,86],{},"Right-click on the ",[83,84,85],"code",{},"L2ST4.tt"," file, choose Properties and set the Custom Tool to blank",[32,88,89,90,93],{},"Rename ",[83,91,92],{},"DataClasses1.tt"," to the same name as your DBML file (but keeping the .tt extension) and open it",[32,95,96],{},"Click save and watch a freshly generate C# DataContext pop out",[32,98,99],{},"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.",[13,101,102,104],{},[83,103,85],{}," 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.",[13,106,107,108,112],{},"This code should be ",[109,110,111],"strong",{},"treated as a sample"," and hasn’t received much testing yet so feel free to leave comments or feedback here.",[13,114,115],{},"Some places you could take this template:",[29,117,118,121,124,127],{},[32,119,120],{},"Generate an interface for your data context to improve mocking",[32,122,123],{},"Alternative naming and defaults",[32,125,126],{},"Splitting output into multiple files",[32,128,129],{},"New languages",[13,131,132],{},[133,134,135],"em",{},"[)amien",{"title":137,"searchDepth":138,"depth":138,"links":139},"",2,[],"Development",10,"2008-07-23T17:19:35+00:00","[object Object]","md",null,false,{},true,"\u002Fblog\u002F2008\u002Flinq-to-sql-t4-template-reloaded",{"title":5,"description":24},"blog\u002F2008\u002Flinq-to-sql-t4-template-reloaded",[153,154,155,156],".NET","LINQ","T4","C#","\u002Fblog\u002F2008\u002Flinq-to-sql-t4-template-reloaded\u002F",340,"LfpQ6HWCFqQKb7bkD3VZZS_aLr0d44dIivSOSkMpFuI",[161,165,169],{"title":162,"date":163,"url":164},"Transactions in the MongoDB EF Core Provider","2025-10-25","\u002Fblog\u002F2025\u002Fmongodb-explicit-transactions\u002F",{"title":166,"date":167,"url":168},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","\u002Fblog\u002F2025\u002Fmongodb-queryable-encryption\u002F",{"title":170,"date":171,"url":172},"Lazy Loading with EF Core Proxies","2025-04-02","\u002Fblog\u002F2025\u002Fef-proxies\u002F",[],1780900527960]