[{"data":1,"prerenderedAt":1375},["ShallowReactive",2],{"blog:2010:linq-to-sql-tips-and-tricks-3":3,"blogMore-Development":1276,"comments-linq-to-sql-tips-and-tricks-3":1289},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"category":11,"tags":12,"excerpt":17,"body":32,"_type":1268,"_id":1269,"_source":1270,"_file":1271,"_stem":1272,"_extension":1273,"url":1274,"wordCount":1275,"minutes":473,"commentCount":426},"/blog/2010/linq-to-sql-tips-and-tricks-3","2010",false,"en","LINQ to SQL tips and tricks #3","Another set of useful and lesser-known LINQ to SQL techniques.","2010-01-11T20:02:37+00:00","Development",[13,14,15,16],".NET","LINQ","C#","SQL",{"type":18,"children":19},"root",[20,27],{"type":21,"tag":22,"props":23,"children":24},"element","p",{},[25],{"type":26,"value":9},"text",{"type":21,"tag":22,"props":28,"children":29},{},[30],{"type":26,"value":31},"LINQ to SQL supports stored procedures for retrieving entities, insert, update and delete operations, as you know. But you can also use them to perform lazy-loading of navigation properties.",{"type":18,"children":33,"toc":1256},[34,38,45,49,62,217,222,334,339,575,580,618,624,653,660,673,834,855,861,873,1004,1013,1019,1024,1029,1035,1040,1101,1106,1112,1123,1156,1177,1182,1188,1222,1227,1242,1250],{"type":21,"tag":22,"props":35,"children":36},{},[37],{"type":26,"value":9},{"type":21,"tag":39,"props":40,"children":42},"h2",{"id":41},"lazy-loading-with-stored-procedures",[43],{"type":26,"value":44},"Lazy loading with stored procedures",{"type":21,"tag":22,"props":46,"children":47},{},[48],{"type":26,"value":31},{"type":21,"tag":22,"props":50,"children":51},{},[52,54,60],{"type":26,"value":53},"Let’s show an example of a bi-directional relationship between a Post and a Comment. We have two stored procedures shown below, bringing them into the DBML by dragging them from ",{"type":21,"tag":55,"props":56,"children":57},"em",{},[58],{"type":26,"value":59},"Server Explorer",{"type":26,"value":61}," into the LINQ to SQL designer surface, setting the return type property for each to the expected entity (Post and Comment, respectively).",{"type":21,"tag":63,"props":64,"children":69},"pre",{"className":65,"code":66,"language":67,"meta":68,"style":68},"language-sql shiki shiki-themes everforest-light dracula","CREATE PROCEDURE LoadPost (@PostID int) AS SELECT * FROM Posts WHERE ID = @PostID\nCREATE PROCEDURE LoadComments(@PostID int) AS SELECT * FROM Comments WHERE Parent_Post_ID = @PostID\n","sql","",[70],{"type":21,"tag":71,"props":72,"children":73},"code",{"__ignoreMap":68},[74,154],{"type":21,"tag":75,"props":76,"children":79},"span",{"class":77,"line":78},"line",1,[80,86,91,97,103,108,113,118,124,129,134,139,144,149],{"type":21,"tag":75,"props":81,"children":83},{"style":82},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[84],{"type":26,"value":85},"CREATE",{"type":21,"tag":75,"props":87,"children":88},{"style":82},[89],{"type":26,"value":90}," PROCEDURE",{"type":21,"tag":75,"props":92,"children":94},{"style":93},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[95],{"type":26,"value":96}," LoadPost (@PostID ",{"type":21,"tag":75,"props":98,"children":100},{"style":99},"--shiki-default:#3A94C5;--shiki-dark:#FF79C6",[101],{"type":26,"value":102},"int",{"type":21,"tag":75,"props":104,"children":105},{"style":93},[106],{"type":26,"value":107},") ",{"type":21,"tag":75,"props":109,"children":110},{"style":82},[111],{"type":26,"value":112},"AS",{"type":21,"tag":75,"props":114,"children":115},{"style":82},[116],{"type":26,"value":117}," SELECT",{"type":21,"tag":75,"props":119,"children":121},{"style":120},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[122],{"type":26,"value":123}," *",{"type":21,"tag":75,"props":125,"children":126},{"style":82},[127],{"type":26,"value":128}," FROM",{"type":21,"tag":75,"props":130,"children":131},{"style":93},[132],{"type":26,"value":133}," Posts ",{"type":21,"tag":75,"props":135,"children":136},{"style":82},[137],{"type":26,"value":138},"WHERE",{"type":21,"tag":75,"props":140,"children":141},{"style":93},[142],{"type":26,"value":143}," ID ",{"type":21,"tag":75,"props":145,"children":146},{"style":120},[147],{"type":26,"value":148},"=",{"type":21,"tag":75,"props":150,"children":151},{"style":93},[152],{"type":26,"value":153}," @PostID\n",{"type":21,"tag":75,"props":155,"children":157},{"class":77,"line":156},2,[158,162,166,171,175,179,183,187,191,195,200,204,209,213],{"type":21,"tag":75,"props":159,"children":160},{"style":82},[161],{"type":26,"value":85},{"type":21,"tag":75,"props":163,"children":164},{"style":82},[165],{"type":26,"value":90},{"type":21,"tag":75,"props":167,"children":168},{"style":93},[169],{"type":26,"value":170}," LoadComments(@PostID ",{"type":21,"tag":75,"props":172,"children":173},{"style":99},[174],{"type":26,"value":102},{"type":21,"tag":75,"props":176,"children":177},{"style":93},[178],{"type":26,"value":107},{"type":21,"tag":75,"props":180,"children":181},{"style":82},[182],{"type":26,"value":112},{"type":21,"tag":75,"props":184,"children":185},{"style":82},[186],{"type":26,"value":117},{"type":21,"tag":75,"props":188,"children":189},{"style":120},[190],{"type":26,"value":123},{"type":21,"tag":75,"props":192,"children":193},{"style":82},[194],{"type":26,"value":128},{"type":21,"tag":75,"props":196,"children":197},{"style":93},[198],{"type":26,"value":199}," Comments ",{"type":21,"tag":75,"props":201,"children":202},{"style":82},[203],{"type":26,"value":138},{"type":21,"tag":75,"props":205,"children":206},{"style":93},[207],{"type":26,"value":208}," Parent_Post_ID ",{"type":21,"tag":75,"props":210,"children":211},{"style":120},[212],{"type":26,"value":148},{"type":21,"tag":75,"props":214,"children":215},{"style":93},[216],{"type":26,"value":153},{"type":21,"tag":22,"props":218,"children":219},{},[220],{"type":26,"value":221},"This generates two method stubs named LoadPost and LoadComments that we can use to programmatically retrieve entities:",{"type":21,"tag":63,"props":223,"children":227},{"className":224,"code":225,"language":226,"meta":68,"style":68},"language-csharp shiki shiki-themes everforest-light dracula","var post = dataContext.LoadPost(1).First();\nConsole.WriteLine(\"{0}\", post.Title);\n","csharp",[228],{"type":21,"tag":71,"props":229,"children":230},{"__ignoreMap":68},[231,285],{"type":21,"tag":75,"props":232,"children":233},{"class":77,"line":78},[234,239,244,248,253,259,264,270,275,280],{"type":21,"tag":75,"props":235,"children":236},{"style":99},[237],{"type":26,"value":238},"var",{"type":21,"tag":75,"props":240,"children":241},{"style":93},[242],{"type":26,"value":243}," post ",{"type":21,"tag":75,"props":245,"children":246},{"style":120},[247],{"type":26,"value":148},{"type":21,"tag":75,"props":249,"children":250},{"style":93},[251],{"type":26,"value":252}," dataContext.",{"type":21,"tag":75,"props":254,"children":256},{"style":255},"--shiki-default:#8DA101;--shiki-dark:#50FA7B",[257],{"type":26,"value":258},"LoadPost",{"type":21,"tag":75,"props":260,"children":261},{"style":93},[262],{"type":26,"value":263},"(",{"type":21,"tag":75,"props":265,"children":267},{"style":266},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[268],{"type":26,"value":269},"1",{"type":21,"tag":75,"props":271,"children":272},{"style":93},[273],{"type":26,"value":274},").",{"type":21,"tag":75,"props":276,"children":277},{"style":255},[278],{"type":26,"value":279},"First",{"type":21,"tag":75,"props":281,"children":282},{"style":93},[283],{"type":26,"value":284},"();\n",{"type":21,"tag":75,"props":286,"children":287},{"class":77,"line":156},[288,293,298,302,308,314,318,323,329],{"type":21,"tag":75,"props":289,"children":290},{"style":93},[291],{"type":26,"value":292},"Console.",{"type":21,"tag":75,"props":294,"children":295},{"style":255},[296],{"type":26,"value":297},"WriteLine",{"type":21,"tag":75,"props":299,"children":300},{"style":93},[301],{"type":26,"value":263},{"type":21,"tag":75,"props":303,"children":305},{"style":304},"--shiki-default:#8DA101;--shiki-dark:#E9F284",[306],{"type":26,"value":307},"\"",{"type":21,"tag":75,"props":309,"children":311},{"style":310},"--shiki-default:#8DA101;--shiki-dark:#F1FA8C",[312],{"type":26,"value":313},"{0}",{"type":21,"tag":75,"props":315,"children":316},{"style":304},[317],{"type":26,"value":307},{"type":21,"tag":75,"props":319,"children":320},{"style":93},[321],{"type":26,"value":322},", post.",{"type":21,"tag":75,"props":324,"children":326},{"style":325},"--shiki-default:#35A77C;--shiki-dark:#F8F8F2",[327],{"type":26,"value":328},"Title",{"type":21,"tag":75,"props":330,"children":331},{"style":93},[332],{"type":26,"value":333},");\n",{"type":21,"tag":22,"props":335,"children":336},{},[337],{"type":26,"value":338},"Now to replace LINQ to SQL’s lazy-loading query generation. We add methods to the data context subclass with a specific signature.",{"type":21,"tag":63,"props":340,"children":342},{"className":224,"code":341,"language":226,"meta":68,"style":68},"partial class DataClasses1DataContext {\n  protected IEnumerable\u003CComment> LoadComments(Post post) {\n    return this.LoadComments(post.ID);\n  }\n\n  protected Post LoadParentPost(Comment comment) {\n    return this.LoadPost(comment.Post_ID).First();\n  }\n}\n",[343],{"type":21,"tag":71,"props":344,"children":345},{"__ignoreMap":68},[346,370,424,462,471,481,516,558,566],{"type":21,"tag":75,"props":347,"children":348},{"class":77,"line":78},[349,354,359,365],{"type":21,"tag":75,"props":350,"children":351},{"style":120},[352],{"type":26,"value":353},"partial",{"type":21,"tag":75,"props":355,"children":356},{"style":82},[357],{"type":26,"value":358}," class",{"type":21,"tag":75,"props":360,"children":362},{"style":361},"--shiki-default:#3A94C5;--shiki-dark:#8BE9FD",[363],{"type":26,"value":364}," DataClasses1DataContext",{"type":21,"tag":75,"props":366,"children":367},{"style":93},[368],{"type":26,"value":369}," {\n",{"type":21,"tag":75,"props":371,"children":372},{"class":77,"line":156},[373,378,384,389,394,399,404,408,413,419],{"type":21,"tag":75,"props":374,"children":375},{"style":120},[376],{"type":26,"value":377},"  protected",{"type":21,"tag":75,"props":379,"children":381},{"style":380},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[382],{"type":26,"value":383}," IEnumerable",{"type":21,"tag":75,"props":385,"children":386},{"style":93},[387],{"type":26,"value":388},"\u003C",{"type":21,"tag":75,"props":390,"children":391},{"style":380},[392],{"type":26,"value":393},"Comment",{"type":21,"tag":75,"props":395,"children":396},{"style":93},[397],{"type":26,"value":398},"> ",{"type":21,"tag":75,"props":400,"children":401},{"style":255},[402],{"type":26,"value":403},"LoadComments",{"type":21,"tag":75,"props":405,"children":406},{"style":93},[407],{"type":26,"value":263},{"type":21,"tag":75,"props":409,"children":410},{"style":380},[411],{"type":26,"value":412},"Post",{"type":21,"tag":75,"props":414,"children":416},{"style":415},"--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic",[417],{"type":26,"value":418}," post",{"type":21,"tag":75,"props":420,"children":421},{"style":93},[422],{"type":26,"value":423},") {\n",{"type":21,"tag":75,"props":425,"children":427},{"class":77,"line":426},3,[428,433,439,444,448,453,458],{"type":21,"tag":75,"props":429,"children":430},{"style":82},[431],{"type":26,"value":432},"    return",{"type":21,"tag":75,"props":434,"children":436},{"style":435},"--shiki-default:#DF69BA;--shiki-default-font-style:inherit;--shiki-dark:#BD93F9;--shiki-dark-font-style:italic",[437],{"type":26,"value":438}," this",{"type":21,"tag":75,"props":440,"children":441},{"style":93},[442],{"type":26,"value":443},".",{"type":21,"tag":75,"props":445,"children":446},{"style":255},[447],{"type":26,"value":403},{"type":21,"tag":75,"props":449,"children":450},{"style":93},[451],{"type":26,"value":452},"(post.",{"type":21,"tag":75,"props":454,"children":455},{"style":325},[456],{"type":26,"value":457},"ID",{"type":21,"tag":75,"props":459,"children":460},{"style":93},[461],{"type":26,"value":333},{"type":21,"tag":75,"props":463,"children":465},{"class":77,"line":464},4,[466],{"type":21,"tag":75,"props":467,"children":468},{"style":93},[469],{"type":26,"value":470},"  }\n",{"type":21,"tag":75,"props":472,"children":474},{"class":77,"line":473},5,[475],{"type":21,"tag":75,"props":476,"children":478},{"emptyLinePlaceholder":477},true,[479],{"type":26,"value":480},"\n",{"type":21,"tag":75,"props":482,"children":484},{"class":77,"line":483},6,[485,489,494,499,503,507,512],{"type":21,"tag":75,"props":486,"children":487},{"style":120},[488],{"type":26,"value":377},{"type":21,"tag":75,"props":490,"children":491},{"style":380},[492],{"type":26,"value":493}," Post",{"type":21,"tag":75,"props":495,"children":496},{"style":255},[497],{"type":26,"value":498}," LoadParentPost",{"type":21,"tag":75,"props":500,"children":501},{"style":93},[502],{"type":26,"value":263},{"type":21,"tag":75,"props":504,"children":505},{"style":380},[506],{"type":26,"value":393},{"type":21,"tag":75,"props":508,"children":509},{"style":415},[510],{"type":26,"value":511}," comment",{"type":21,"tag":75,"props":513,"children":514},{"style":93},[515],{"type":26,"value":423},{"type":21,"tag":75,"props":517,"children":519},{"class":77,"line":518},7,[520,524,528,532,536,541,546,550,554],{"type":21,"tag":75,"props":521,"children":522},{"style":82},[523],{"type":26,"value":432},{"type":21,"tag":75,"props":525,"children":526},{"style":435},[527],{"type":26,"value":438},{"type":21,"tag":75,"props":529,"children":530},{"style":93},[531],{"type":26,"value":443},{"type":21,"tag":75,"props":533,"children":534},{"style":255},[535],{"type":26,"value":258},{"type":21,"tag":75,"props":537,"children":538},{"style":93},[539],{"type":26,"value":540},"(comment.",{"type":21,"tag":75,"props":542,"children":543},{"style":325},[544],{"type":26,"value":545},"Post_ID",{"type":21,"tag":75,"props":547,"children":548},{"style":93},[549],{"type":26,"value":274},{"type":21,"tag":75,"props":551,"children":552},{"style":255},[553],{"type":26,"value":279},{"type":21,"tag":75,"props":555,"children":556},{"style":93},[557],{"type":26,"value":284},{"type":21,"tag":75,"props":559,"children":561},{"class":77,"line":560},8,[562],{"type":21,"tag":75,"props":563,"children":564},{"style":93},[565],{"type":26,"value":470},{"type":21,"tag":75,"props":567,"children":569},{"class":77,"line":568},9,[570],{"type":21,"tag":75,"props":571,"children":572},{"style":93},[573],{"type":26,"value":574},"}\n",{"type":21,"tag":22,"props":576,"children":577},{},[578],{"type":26,"value":579},"To get the signature of the method names right:",{"type":21,"tag":581,"props":582,"children":583},"ol",{},[584,590,603,608,613],{"type":21,"tag":585,"props":586,"children":587},"li",{},[588],{"type":26,"value":589},"Visibility can be anything (protected or private recommended)",{"type":21,"tag":585,"props":591,"children":592},{},[593,595,601],{"type":26,"value":594},"Return type must be the type of the other side of the association (wrapped in ",{"type":21,"tag":71,"props":596,"children":598},{"className":597},[],[599],{"type":26,"value":600},"IEnumerable\u003CT>",{"type":26,"value":602}," when that side can be many)",{"type":21,"tag":585,"props":604,"children":605},{},[606],{"type":26,"value":607},"Method name must start with the word “Load”",{"type":21,"tag":585,"props":609,"children":610},{},[611],{"type":26,"value":612},"Method name must then continue with the name of the navigation property you want to intercept",{"type":21,"tag":585,"props":614,"children":615},{},[616],{"type":26,"value":617},"Parameter type must be the type that has the named navigation property (step 4)",{"type":21,"tag":39,"props":619,"children":621},{"id":620},"storing-and-retrieving-binary-files",[622],{"type":26,"value":623},"Storing and retrieving binary files",{"type":21,"tag":22,"props":625,"children":626},{},[627,629,635,637,643,645,651],{"type":26,"value":628},"LINQ to SQL supports the SQL Server’s ",{"type":21,"tag":71,"props":630,"children":632},{"className":631},[],[633],{"type":26,"value":634},"varbinary",{"type":26,"value":636}," type but storing something practical like a file in there isn’t clearly documented. Map your ",{"type":21,"tag":71,"props":638,"children":640},{"className":639},[],[641],{"type":26,"value":642},"varbinary(max)",{"type":26,"value":644}," column from your table into your entity, which exposes the column as using the ",{"type":21,"tag":71,"props":646,"children":648},{"className":647},[],[649],{"type":26,"value":650},"System.Data.Linq.Binary",{"type":26,"value":652}," type (effectively a wrapper for a byte array but better change tracking).",{"type":21,"tag":654,"props":655,"children":657},"h3",{"id":656},"file-to-database",[658],{"type":26,"value":659},"File to database",{"type":21,"tag":22,"props":661,"children":662},{},[663,665,671],{"type":26,"value":664},"To store a file in the database, read those bytes in and assign them to the property (",{"type":21,"tag":71,"props":666,"children":668},{"className":667},[],[669],{"type":26,"value":670},"Binary",{"type":26,"value":672}," knows how to create itself from a byte array automatically). e.g.",{"type":21,"tag":63,"props":674,"children":676},{"className":224,"code":675,"language":226,"meta":68,"style":68},"var readPath = @\"c:\\test.jpg\";\nvar storedFile = new StoredFile();\nstoredFile.Binary = File.ReadAllBytes(readPath);\nstoredFile.FileName = Path.GetFileName(readPath);\ndata.StoredFiles.InsertOnSubmit(storedFile);\n",[677],{"type":21,"tag":71,"props":678,"children":679},{"__ignoreMap":68},[680,715,745,777,807],{"type":21,"tag":75,"props":681,"children":682},{"class":77,"line":78},[683,687,692,696,701,706,710],{"type":21,"tag":75,"props":684,"children":685},{"style":99},[686],{"type":26,"value":238},{"type":21,"tag":75,"props":688,"children":689},{"style":93},[690],{"type":26,"value":691}," readPath ",{"type":21,"tag":75,"props":693,"children":694},{"style":120},[695],{"type":26,"value":148},{"type":21,"tag":75,"props":697,"children":698},{"style":304},[699],{"type":26,"value":700}," @\"",{"type":21,"tag":75,"props":702,"children":703},{"style":310},[704],{"type":26,"value":705},"c:\\test.jpg",{"type":21,"tag":75,"props":707,"children":708},{"style":304},[709],{"type":26,"value":307},{"type":21,"tag":75,"props":711,"children":712},{"style":93},[713],{"type":26,"value":714},";\n",{"type":21,"tag":75,"props":716,"children":717},{"class":77,"line":156},[718,722,727,731,736,741],{"type":21,"tag":75,"props":719,"children":720},{"style":99},[721],{"type":26,"value":238},{"type":21,"tag":75,"props":723,"children":724},{"style":93},[725],{"type":26,"value":726}," storedFile ",{"type":21,"tag":75,"props":728,"children":729},{"style":120},[730],{"type":26,"value":148},{"type":21,"tag":75,"props":732,"children":733},{"style":82},[734],{"type":26,"value":735}," new",{"type":21,"tag":75,"props":737,"children":738},{"style":380},[739],{"type":26,"value":740}," StoredFile",{"type":21,"tag":75,"props":742,"children":743},{"style":93},[744],{"type":26,"value":284},{"type":21,"tag":75,"props":746,"children":747},{"class":77,"line":426},[748,753,757,762,767,772],{"type":21,"tag":75,"props":749,"children":750},{"style":93},[751],{"type":26,"value":752},"storedFile.",{"type":21,"tag":75,"props":754,"children":755},{"style":325},[756],{"type":26,"value":670},{"type":21,"tag":75,"props":758,"children":759},{"style":120},[760],{"type":26,"value":761}," =",{"type":21,"tag":75,"props":763,"children":764},{"style":93},[765],{"type":26,"value":766}," File.",{"type":21,"tag":75,"props":768,"children":769},{"style":255},[770],{"type":26,"value":771},"ReadAllBytes",{"type":21,"tag":75,"props":773,"children":774},{"style":93},[775],{"type":26,"value":776},"(readPath);\n",{"type":21,"tag":75,"props":778,"children":779},{"class":77,"line":464},[780,784,789,793,798,803],{"type":21,"tag":75,"props":781,"children":782},{"style":93},[783],{"type":26,"value":752},{"type":21,"tag":75,"props":785,"children":786},{"style":325},[787],{"type":26,"value":788},"FileName",{"type":21,"tag":75,"props":790,"children":791},{"style":120},[792],{"type":26,"value":761},{"type":21,"tag":75,"props":794,"children":795},{"style":93},[796],{"type":26,"value":797}," Path.",{"type":21,"tag":75,"props":799,"children":800},{"style":255},[801],{"type":26,"value":802},"GetFileName",{"type":21,"tag":75,"props":804,"children":805},{"style":93},[806],{"type":26,"value":776},{"type":21,"tag":75,"props":808,"children":809},{"class":77,"line":473},[810,815,820,824,829],{"type":21,"tag":75,"props":811,"children":812},{"style":93},[813],{"type":26,"value":814},"data.",{"type":21,"tag":75,"props":816,"children":817},{"style":325},[818],{"type":26,"value":819},"StoredFiles",{"type":21,"tag":75,"props":821,"children":822},{"style":93},[823],{"type":26,"value":443},{"type":21,"tag":75,"props":825,"children":826},{"style":255},[827],{"type":26,"value":828},"InsertOnSubmit",{"type":21,"tag":75,"props":830,"children":831},{"style":93},[832],{"type":26,"value":833},"(storedFile);\n",{"type":21,"tag":22,"props":835,"children":836},{},[837,839,845,847,853],{"type":26,"value":838},"I recommend storing the file name as well as the binary contents for two reasons. Firstly, writing the file back to disk or streaming it to a browser requires you know the file type (eg. ",{"type":21,"tag":71,"props":840,"children":842},{"className":841},[],[843],{"type":26,"value":844},".jpg",{"type":26,"value":846}," or ",{"type":21,"tag":71,"props":848,"children":850},{"className":849},[],[851],{"type":26,"value":852},"image/jpeg",{"type":26,"value":854},"), and, secondly, nobody likes downloading a file called ‘download’ or ‘1’.)",{"type":21,"tag":654,"props":856,"children":858},{"id":857},"database-to-file",[859],{"type":26,"value":860},"Database to file",{"type":21,"tag":22,"props":862,"children":863},{},[864,866,871],{"type":26,"value":865},"Writing the file back to disk is just as easy although, you have to use the ToArray() method of ",{"type":21,"tag":71,"props":867,"children":869},{"className":868},[],[870],{"type":26,"value":650},{"type":26,"value":872}," to turn it back into a byte array.",{"type":21,"tag":63,"props":874,"children":876},{"className":224,"code":875,"language":226,"meta":68,"style":68},"var writePath = @\"c:\\temp\";\nvar storedFile = data.StoredFiles.First();\nFile.WriteAllBytes(Path.Combine(writePath, storedFile.FileName), storedFile.Binary.ToArray());\n",[877],{"type":21,"tag":71,"props":878,"children":879},{"__ignoreMap":68},[880,913,949],{"type":21,"tag":75,"props":881,"children":882},{"class":77,"line":78},[883,887,892,896,900,905,909],{"type":21,"tag":75,"props":884,"children":885},{"style":99},[886],{"type":26,"value":238},{"type":21,"tag":75,"props":888,"children":889},{"style":93},[890],{"type":26,"value":891}," writePath ",{"type":21,"tag":75,"props":893,"children":894},{"style":120},[895],{"type":26,"value":148},{"type":21,"tag":75,"props":897,"children":898},{"style":304},[899],{"type":26,"value":700},{"type":21,"tag":75,"props":901,"children":902},{"style":310},[903],{"type":26,"value":904},"c:\\temp",{"type":21,"tag":75,"props":906,"children":907},{"style":304},[908],{"type":26,"value":307},{"type":21,"tag":75,"props":910,"children":911},{"style":93},[912],{"type":26,"value":714},{"type":21,"tag":75,"props":914,"children":915},{"class":77,"line":156},[916,920,924,928,933,937,941,945],{"type":21,"tag":75,"props":917,"children":918},{"style":99},[919],{"type":26,"value":238},{"type":21,"tag":75,"props":921,"children":922},{"style":93},[923],{"type":26,"value":726},{"type":21,"tag":75,"props":925,"children":926},{"style":120},[927],{"type":26,"value":148},{"type":21,"tag":75,"props":929,"children":930},{"style":93},[931],{"type":26,"value":932}," data.",{"type":21,"tag":75,"props":934,"children":935},{"style":325},[936],{"type":26,"value":819},{"type":21,"tag":75,"props":938,"children":939},{"style":93},[940],{"type":26,"value":443},{"type":21,"tag":75,"props":942,"children":943},{"style":255},[944],{"type":26,"value":279},{"type":21,"tag":75,"props":946,"children":947},{"style":93},[948],{"type":26,"value":284},{"type":21,"tag":75,"props":950,"children":951},{"class":77,"line":426},[952,957,962,967,972,977,981,986,990,994,999],{"type":21,"tag":75,"props":953,"children":954},{"style":93},[955],{"type":26,"value":956},"File.",{"type":21,"tag":75,"props":958,"children":959},{"style":255},[960],{"type":26,"value":961},"WriteAllBytes",{"type":21,"tag":75,"props":963,"children":964},{"style":93},[965],{"type":26,"value":966},"(Path.",{"type":21,"tag":75,"props":968,"children":969},{"style":255},[970],{"type":26,"value":971},"Combine",{"type":21,"tag":75,"props":973,"children":974},{"style":93},[975],{"type":26,"value":976},"(writePath, storedFile.",{"type":21,"tag":75,"props":978,"children":979},{"style":325},[980],{"type":26,"value":788},{"type":21,"tag":75,"props":982,"children":983},{"style":93},[984],{"type":26,"value":985},"), storedFile.",{"type":21,"tag":75,"props":987,"children":988},{"style":325},[989],{"type":26,"value":670},{"type":21,"tag":75,"props":991,"children":992},{"style":93},[993],{"type":26,"value":443},{"type":21,"tag":75,"props":995,"children":996},{"style":255},[997],{"type":26,"value":998},"ToArray",{"type":21,"tag":75,"props":1000,"children":1001},{"style":93},[1002],{"type":26,"value":1003},"());\n",{"type":21,"tag":1005,"props":1006,"children":1007},"blockquote",{},[1008],{"type":21,"tag":22,"props":1009,"children":1010},{},[1011],{"type":26,"value":1012},"Always ensure when writing to the file system based on data that filenames are sanitized! You don’t want users overwriting critical files on your system.",{"type":21,"tag":39,"props":1014,"children":1016},{"id":1015},"multiple-databases-with-a-single-context",[1017],{"type":26,"value":1018},"Multiple databases with a single context",{"type":21,"tag":22,"props":1020,"children":1021},{},[1022],{"type":26,"value":1023},"Contrary to popular belief, you can, in fact, access entities from multiple databases with a single data context - providing they live on the same server. This is unsupported, but I’ve used it on my own projects without issue.",{"type":21,"tag":22,"props":1025,"children":1026},{},[1027],{"type":26,"value":1028},"The first part is the tricky bit which involves getting the definition of your entity into your DBML. You have two options here:",{"type":21,"tag":654,"props":1030,"children":1032},{"id":1031},"create-a-temporary-view",[1033],{"type":26,"value":1034},"Create a temporary view",{"type":21,"tag":22,"props":1036,"children":1037},{},[1038],{"type":26,"value":1039},"If you have the rights, you can temporarily create views in your primary database for each table in your non-primary database.",{"type":21,"tag":63,"props":1041,"children":1043},{"className":65,"code":1042,"language":67,"meta":68,"style":68},"CREATE VIEW MyOtherTable AS SELECT * FROM MyOtherDatabase.dbo.MyOtherTable\n",[1044],{"type":21,"tag":71,"props":1045,"children":1046},{"__ignoreMap":68},[1047],{"type":21,"tag":75,"props":1048,"children":1049},{"class":77,"line":78},[1050,1054,1059,1064,1069,1073,1077,1081,1087,1091,1096],{"type":21,"tag":75,"props":1051,"children":1052},{"style":82},[1053],{"type":26,"value":85},{"type":21,"tag":75,"props":1055,"children":1056},{"style":82},[1057],{"type":26,"value":1058}," VIEW",{"type":21,"tag":75,"props":1060,"children":1061},{"style":255},[1062],{"type":26,"value":1063}," MyOtherTable",{"type":21,"tag":75,"props":1065,"children":1066},{"style":82},[1067],{"type":26,"value":1068}," AS",{"type":21,"tag":75,"props":1070,"children":1071},{"style":82},[1072],{"type":26,"value":117},{"type":21,"tag":75,"props":1074,"children":1075},{"style":120},[1076],{"type":26,"value":123},{"type":21,"tag":75,"props":1078,"children":1079},{"style":82},[1080],{"type":26,"value":128},{"type":21,"tag":75,"props":1082,"children":1084},{"style":1083},"--shiki-default:#5C6A72;--shiki-dark:#BD93F9",[1085],{"type":26,"value":1086}," MyOtherDatabase",{"type":21,"tag":75,"props":1088,"children":1089},{"style":93},[1090],{"type":26,"value":443},{"type":21,"tag":75,"props":1092,"children":1093},{"style":1083},[1094],{"type":26,"value":1095},"dbo",{"type":21,"tag":75,"props":1097,"children":1098},{"style":93},[1099],{"type":26,"value":1100},".MyOtherTable\n",{"type":21,"tag":22,"props":1102,"children":1103},{},[1104],{"type":26,"value":1105},"Once views are created, add them to your DBML by dragging them from Server Explorer into the LINQ to SQL designer surface and delete the views you created from the database.",{"type":21,"tag":654,"props":1107,"children":1109},{"id":1108},"create-a-temporary-dbml",[1110],{"type":26,"value":1111},"Create a temporary DBML",{"type":21,"tag":22,"props":1113,"children":1114},{},[1115,1117,1121],{"type":26,"value":1116},"If you can’t or don’t want to create temporary views, then add a second (temporary) LINQ to SQL classes file (DBML) to your project. Use ",{"type":21,"tag":55,"props":1118,"children":1119},{},[1120],{"type":26,"value":59},{"type":26,"value":1122}," to find your secondary database and drag all the tables you want to access to the LINQ to SQL designer surface.",{"type":21,"tag":22,"props":1124,"children":1125},{},[1126,1128,1133,1135,1140,1142,1147,1149,1154],{"type":26,"value":1127},"Now save & close open files and use the right-mouse-button context menu to ",{"type":21,"tag":55,"props":1129,"children":1130},{},[1131],{"type":26,"value":1132},"Open With…",{"type":26,"value":1134}," and choose ",{"type":21,"tag":55,"props":1136,"children":1137},{},[1138],{"type":26,"value":1139},"XML Editor",{"type":26,"value":1141}," on your original DBML and the new temporary one. Head to the ",{"type":21,"tag":55,"props":1143,"children":1144},{},[1145],{"type":26,"value":1146},"Window",{"type":26,"value":1148}," menu and select ",{"type":21,"tag":55,"props":1150,"children":1151},{},[1152],{"type":26,"value":1153},"New Vertical Tab Group",{"type":26,"value":1155}," to make the next step easier.",{"type":21,"tag":22,"props":1157,"children":1158},{},[1159,1161,1167,1169,1175],{"type":26,"value":1160},"Looking through the DBML you see each entity has a ",{"type":21,"tag":71,"props":1162,"children":1164},{"className":1163},[],[1165],{"type":26,"value":1166},"\u003CTable>",{"type":26,"value":1168}," block inside the ",{"type":21,"tag":71,"props":1170,"children":1172},{"className":1171},[],[1173],{"type":26,"value":1174},"\u003CDatabase>",{"type":26,"value":1176},". Select all the Table tags and their children (but not Database or Connection) and copy/paste them into your existing DBML file. Then close the files and check all looks well in the designer again.",{"type":21,"tag":22,"props":1178,"children":1179},{},[1180],{"type":26,"value":1181},"If it does, delete the temporary DBML file you created. If not, go back and check the DBML file for duplicate names, mismatched XML etc.",{"type":21,"tag":654,"props":1183,"children":1185},{"id":1184},"finally-the-easy-bit",[1186],{"type":26,"value":1187},"Finally, the easy bit",{"type":21,"tag":22,"props":1189,"children":1190},{},[1191,1193,1198,1200,1205,1207,1213,1215,1221],{"type":26,"value":1192},"Open the designer and for each table that comes from the other database select it and change the ",{"type":21,"tag":55,"props":1194,"children":1195},{},[1196],{"type":26,"value":1197},"Source",{"type":26,"value":1199}," property in the ",{"type":21,"tag":55,"props":1201,"children":1202},{},[1203],{"type":26,"value":1204},"Properties",{"type":26,"value":1206}," window from ",{"type":21,"tag":71,"props":1208,"children":1210},{"className":1209},[],[1211],{"type":26,"value":1212},"dbo.MyOtherTable",{"type":26,"value":1214}," to ",{"type":21,"tag":71,"props":1216,"children":1218},{"className":1217},[],[1219],{"type":26,"value":1220},"MyOtherDatabase.dbo.MyOtherTable",{"type":26,"value":443},{"type":21,"tag":22,"props":1223,"children":1224},{},[1225],{"type":26,"value":1226},"Hit play and run!",{"type":21,"tag":1005,"props":1228,"children":1229},{},[1230],{"type":21,"tag":22,"props":1231,"children":1232},{},[1233,1235],{"type":26,"value":1234},"Check out ",{"type":21,"tag":1236,"props":1237,"children":1239},"a",{"href":1238},"/blog/2009/linq-to-sql-tips-and-tricks-1/",[1240],{"type":26,"value":1241},"part 1 of LINQ to SQL tips",{"type":21,"tag":22,"props":1243,"children":1244},{},[1245],{"type":21,"tag":55,"props":1246,"children":1247},{},[1248],{"type":26,"value":1249},"[)amien",{"type":21,"tag":1251,"props":1252,"children":1253},"style",{},[1254],{"type":26,"value":1255},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":68,"searchDepth":156,"depth":156,"links":1257},[1258,1259,1263],{"id":41,"depth":156,"text":44},{"id":620,"depth":156,"text":623,"children":1260},[1261,1262],{"id":656,"depth":426,"text":659},{"id":857,"depth":426,"text":860},{"id":1015,"depth":156,"text":1018,"children":1264},[1265,1266,1267],{"id":1031,"depth":426,"text":1034},{"id":1108,"depth":426,"text":1111},{"id":1184,"depth":426,"text":1187},"markdown","content:blog:2010:linq-to-sql-tips-and-tricks-3.md","content","blog/2010/linq-to-sql-tips-and-tricks-3.md","blog/2010/linq-to-sql-tips-and-tricks-3","md","/blog/2010/linq-to-sql-tips-and-tricks-3/",947,[1277,1281,1285],{"title":1278,"date":1279,"url":1280},"HTML5 Video Cheatsheet: Optimizing videos for the web","2025-12-05T00:00:00Z","/blog/2025/html5-video-cheatsheet/",{"title":1282,"date":1283,"url":1284},"Transactions in the MongoDB EF Core Provider","2025-10-25","/blog/2025/mongodb-explicit-transactions/",{"title":1286,"date":1287,"url":1288},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","/blog/2025/mongodb-queryable-encryption/",[1290,1329,1350],{"_path":1291,"_dir":1292,"_draft":6,"_partial":6,"_locale":7,"title":1293,"description":68,"id":1294,"name":1295,"email":1296,"avatar":1297,"date":1298,"body":1299,"_type":1268,"_id":1326,"_source":1270,"_file":1327,"_stem":1328,"_extension":1273},"/comments/linq-to-sql-tips-and-tricks-3/41930","linq-to-sql-tips-and-tricks-3","41930",41930,"wes","wsmith@brtrc.com","https://www.gravatar.com/avatar/70b6e33a8585bcffea3692703ad6a808?r=pg&d=retro","2010-06-02T11:35:38",{"type":18,"children":1300,"toc":1324},[1301,1309,1314,1319],{"type":21,"tag":1005,"props":1302,"children":1303},{},[1304],{"type":21,"tag":22,"props":1305,"children":1306},{},[1307],{"type":26,"value":1308},"replace LINQ to SQL’s lazy-loading query generation...",{"type":21,"tag":22,"props":1310,"children":1311},{},[1312],{"type":26,"value":1313},"Thank You for this tip!! It was a HUGE help!",{"type":21,"tag":22,"props":1315,"children":1316},{},[1317],{"type":26,"value":1318},"Due to the table columns being encrypted with a pass phrase I need to do selects from a UDF instead of directly from the table. While functions for insert/update/delete are supported, using a function for select is not.",{"type":21,"tag":22,"props":1320,"children":1321},{},[1322],{"type":26,"value":1323},"I was able to use my own queries for direct actions on the table but couldn't figure out how to prevent it from doing direct access to the table when displaying related data. Your tip fixed that.",{"title":68,"searchDepth":156,"depth":156,"links":1325},[],"content:comments:linq-to-sql-tips-and-tricks-3:41930.md","comments/linq-to-sql-tips-and-tricks-3/41930.md","comments/linq-to-sql-tips-and-tricks-3/41930",{"_path":1330,"_dir":1292,"_draft":6,"_partial":6,"_locale":7,"title":1331,"description":1332,"id":1333,"name":1334,"email":1335,"avatar":1336,"url":1337,"date":1338,"body":1339,"_type":1268,"_id":1347,"_source":1270,"_file":1348,"_stem":1349,"_extension":1273},"/comments/linq-to-sql-tips-and-tricks-3/34784","34784","The LINQ to SQL designer will not allow you to add two tables from different databases.",34784,"Damien Guard","damien@envytech.co.uk","https://www.gravatar.com/avatar/dc72963e7279d34c85ed4c0b731ce5a9?r=pg&d=retro","https://damieng.com/","2010-01-18T08:49:33",{"type":18,"children":1340,"toc":1345},[1341],{"type":21,"tag":22,"props":1342,"children":1343},{},[1344],{"type":26,"value":1332},{"title":68,"searchDepth":156,"depth":156,"links":1346},[],"content:comments:linq-to-sql-tips-and-tricks-3:34784.md","comments/linq-to-sql-tips-and-tricks-3/34784.md","comments/linq-to-sql-tips-and-tricks-3/34784",{"_path":1351,"_dir":1292,"_draft":6,"_partial":6,"_locale":7,"title":1352,"description":1353,"id":1354,"name":1355,"email":1356,"avatar":1357,"date":1358,"body":1359,"_type":1268,"_id":1372,"_source":1270,"_file":1373,"_stem":1374,"_extension":1273},"/comments/linq-to-sql-tips-and-tricks-3/34780","34780","You do not have to do steps 'Create a temporary view' and 'Create a temporary DBML'. Just add tablem from other DB in server explorer into existing DBML file and repeat step 'Finally, the easy bit'. At least thats the way I do it :)",34780,"mjoksa","mjoksa@gmail.com","https://www.gravatar.com/avatar/7f539f13a37768a5d38af33df68cc133?r=pg&d=retro","2010-01-18T08:00:05",{"type":18,"children":1360,"toc":1370},[1361,1365],{"type":21,"tag":22,"props":1362,"children":1363},{},[1364],{"type":26,"value":1353},{"type":21,"tag":22,"props":1366,"children":1367},{},[1368],{"type":26,"value":1369},"Cheers",{"title":68,"searchDepth":156,"depth":156,"links":1371},[],"content:comments:linq-to-sql-tips-and-tricks-3:34780.md","comments/linq-to-sql-tips-and-tricks-3/34780.md","comments/linq-to-sql-tips-and-tricks-3/34780",1779264584456]