Using System.Data.Linq in a Razor view

You need to import the namespace into your view by adding @using System.Data.Linq at the top of your view. However if you want it in all your views then you need to add <add namespace=”System.Data.Linq” /> to the web.config in your Views folder: <system.web.webPages.razor> <host factoryType=”System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35″ /> <pages pageBaseType=”System.Web.Mvc.WebViewPage”> <namespaces> <add … Read more

T4 template to Generate Enums

I have written one for my needs that converts a lookup table of your choice to an enum: Put this code inside an EnumGenerator.ttinclude file: <#@ template debug=”true” hostSpecific=”true” #> <#@ output extension=”.generated.cs” #> <#@ Assembly Name=”System.Data” #> <#@ import namespace=”System.Data” #> <#@ import namespace=”System.Data.SqlClient” #> <#@ import namespace=”System.IO” #> <#@ import namespace=”System.Text.RegularExpressions” #> <# … Read more

C# Linq-to-Sql – Should DataContext be disposed using IDisposable

Unlike most types which implement IDisposable, DataContext doesn’t really need disposing – at least not in most cases. I asked Matt Warren about this design decision, and here was his response: There are a few reasons we implemented IDisposable: If application logic needs to hold onto an entity beyond when the DataContext is expected to … Read more

NOLOCK with Linq to SQL

Yes it is, so here’s the entry from my blog: The NOLOCK hint is essentially the same as wrapping a query in a transaction whose “isolation level” is set to “read uncommitted”. It means that the query doesn’t care if stuff is in the process of being written to the rows it’s reading from – … Read more

ASP.MVC: Repository that reflects IQueryable but not Linq to SQL, DDD How To question

Assuming that your LINQ to SQL (L2S) classes are auto-generated and reflects your underlying database, the short answer is: Don’t expose IQueryable of any of your L2S classes – it would be a Leaky Abstraction. The slightly longer answer: The whole point of Repositories is to hide data access behind an abstraction so that you … Read more

Benchmark Linq2SQL, Subsonic2, Subsonic3 – Any other ideas to make them faster?

Aristos, here’s the thing I’m having an issue with. You posted a question to our groups and we had a nice, 23-long email exchange. You insisted that SubSonic 3 has problems and you asked me why I haven’t fixed this “slow, broken tool”. I tried to explain to you that, with 233 tables, SubSonic has … Read more