[{"data":1,"prerenderedAt":182},["ShallowReactive",2],{"blog:2008:experimental-linq-to-sql-template-for-t4":3,"blogMore-Development":168,"comments-experimental-linq-to-sql-template-for-t4":181},{"id":4,"title":5,"body":6,"category":147,"commentCount":146,"date":148,"description":149,"excerpt":150,"extension":151,"filenames":152,"hidden":153,"image":152,"meta":154,"minutes":143,"navigation":155,"path":156,"seo":157,"showCategory":152,"stem":158,"tags":159,"updated":152,"url":165,"wordCount":166,"__hash__":167},"content\u002Fblog\u002F2008\u002Fexperimental-linq-to-sql-template-for-t4.md","Experimental LINQ to SQL template",{"type":7,"value":8,"toc":141},"minimark",[9,22,25,35,38,46,53,56,84,89,136],[10,11,12],"blockquote",{},[13,14,15,16,21],"p",{},"A ",[17,18,20],"a",{"href":19},"\u002Fblog\u002F2009\u002Flinq-to-sql-templates-updated-now-on-codeplex\u002F","newer version of this LINQ to SQL template"," is available",[13,23,24],{},"While SQLMetal does a good job of turning your SQL schema into a set of classes for you it doesn’t let you customize the code generation process.",[13,26,27,28,34],{},"Usefully there is now a templating system built into Visual Studio 2008 called ",[17,29,33],{"href":30,"rel":31},"https:\u002F\u002Fmsdn.microsoft.com\u002Fen-us\u002Flibrary\u002Fbb126445.aspx",[32],"nofollow","Text Templates"," (T4 for short).",[13,36,37],{},"Here is a short (369 line) experimental proof-of-concept T4 template I wrote last night that will generate a data context and associated entity classes as a starting point similar to that produced by SQLMetal.",[13,39,40,41,45],{},"Once downloaded unzip and drop the DataContext.cs.tt into your project and edit line 17 to set the connection string. You can also edit lines 18 and 19 to set the namespace and class name. The lightweight wrappers around database, table and column can be found at the end of the file; they simply wrap the SQL Server ",[42,43,44],"code",{},"Information_Schema"," views as briefly as possible.",[13,47,48,49,52],{},"Within seconds Visual Studio should have created a code-behind file for the ",[42,50,51],{},"DataContext"," named DataContext.cs.cs with your generated code ready to use :) If you don’t like the way the template generates your context you can change it :)",[13,54,55],{},"There are limitations with this experimental proof-of-concept including: ",[57,58,59,60,59,64,59,67,59,70,59,78,59,81],"ul",{},"\n  ",[61,62,63],"li",{},"\n    Processes all and only tables in the database (no views or SP’s)\n  ",[61,65,66],{},"\n    Foreign-key relationships are not implemented\n  ",[61,68,69],{},"\n    Column attributes for `IsDbGenerated`, `UpdateCheck` and `AutoSync` not implemented\n  ",[61,71,72,73,77],{},"\n    C# only (",[17,74,76],{"href":75},"https:\u002F\u002Fwww.thedatafarm.com\u002Fblog\u002F","sorry Julie",")\n  ",[61,79,80],{},"\n    Plural and singular naming rules are incomplete\n  ",[61,82,83],{},"\n    Can’t modify schema as you could with a designer stage\n  ",[85,86,88],"h3",{"id":87},"learn-more-about-t4","Learn more about T4!",[57,90,59,91,59,99,59,117,59,129],{},[61,92,93,94,98],{},"\n    Watch a ",[17,95,97],{"href":96},"https:\u002F\u002Fmsdn.microsoft.com\u002Fen-us\u002Fvstudio\u002Fcc308634.aspx","screen-cast about T4 in action"," (download, embedded player is awful)\n  ",[61,100,101,102,106,107,111,112,116],{},"\n    Grab ",[17,103,105],{"href":104},"https:\u002F\u002Fwww.t4editor.net\u002F","T4 Editor"," for IntelliSense within T4 and ",[17,108,110],{"href":109},"https:\u002F\u002Fdotnet.org.za\u002Fhiltong\u002Farchive\u002F2008\u002F02\u002F18\u002Ft4-template-items.aspx","T4 Template Items"," for ",[113,114,115],"em",{},"Add New Item…"," support\n  ",[61,118,119,120,124,125,59],{},"\n    Check out ",[17,121,123],{"href":122},"https:\u002F\u002Fwww.olegsych.com\u002F","Oleg Sych’s blog"," for great T4 articles including how to ",[17,126,128],{"href":127},"https:\u002F\u002Fwww.olegsych.com\u002F2008\u002F02\u002Ft4-template-directive\u002F","enable the 3.5 compiler for templates",[61,130,131,132,59],{},"\n    Learn about ",[17,133,135],{"href":134},"https:\u002F\u002Fblogs.msdn.com\u002Fgarethj\u002Farchive\u002F2006\u002F01\u002F09\u002Ft4debugging.aspx","T4 debugging",[13,137,138],{},[113,139,140],{},"[)amien",{"title":142,"searchDepth":143,"depth":143,"links":144},"",2,[145],{"id":87,"depth":146,"text":88},3,"Development","2008-06-25T22:12:46+00:00","A 369-line T4 template that generates a LINQ to SQL DataContext and entity classes from a SQL Server schema, offered as a proof-of-concept alternative to the un-customisable SQLMetal tool.","[object Object]","md",null,false,{},true,"\u002Fblog\u002F2008\u002Fexperimental-linq-to-sql-template-for-t4",{"title":5,"description":149},"blog\u002F2008\u002Fexperimental-linq-to-sql-template-for-t4",[160,161,162,163,164],".NET","C#","LINQ","T4","Visual Studio","\u002Fblog\u002F2008\u002Fexperimental-linq-to-sql-template-for-t4\u002F",304,"hH6JNJ2YNad_ej2-_-veZ-OKRpS9zU1t78e2m4fv1a0",[169,173,177],{"title":170,"date":171,"url":172},"Transactions in the MongoDB EF Core Provider","2025-10-25","\u002Fblog\u002F2025\u002Fmongodb-explicit-transactions\u002F",{"title":174,"date":175,"url":176},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","\u002Fblog\u002F2025\u002Fmongodb-queryable-encryption\u002F",{"title":178,"date":179,"url":180},"Lazy Loading with EF Core Proxies","2025-04-02","\u002Fblog\u002F2025\u002Fef-proxies\u002F",[],1780900528183]