Compare Linq to SQL with NHibernate

Detail instructions are on ScottGu’s blog. Honestly, Linq to SQL’s GUI is much easier to use for NTier dummies, I was surprised by its Stored Procedure support, just by drag and drop, then through ‘configure behavvior…’->’customize’, all the parameters of stored procedure are shown in the list! The amazing part is, for update sp, the original values are included.

Cool! Easy, and the data reading is very fast. (I know I haven’t optimize my NHibernate code very well. ) Now my only concern is, if Linq to SQL always generates DAL and Entities together, then is it possible to unit test entity object only?

One bug in Linq to SQL I found so far, right click menu, view code, I got an error message says:

The name “MyProjectName.MyModuleName” is not a valid identifier. Please choose a different name.

I had to change project name to MyProjectName_MyModuleName to avoid this error. How come Microsoft doesn’t link dot appear in project name?

Another trick is, our stored procedure need developer passing user login for audit purpose. In Linq to Sql Gui view, only class property can be inserted as sp’s parameter. So I added an extra property called UserLogin into my model/table, then delete the auto gen code in XXX.designer.cs, add following lines to partial class:

public partial class MyClass

public string UserLogin
return Environment.UserDomainName + “\\” + Environment.UserName;
//or return System.Security.Principal.WindowsIdentity.GetCurrent().Name;


But the piece of code might be override by dbml regenerate. Instead, I moved those code into a BaseTable class, then change the entity class inherited from this BaseTable. Afterwards, I can change the UpdateEntity method in desinger.cs file, replace the original default(string) to obj.UserLogin. This method won’t be overrode by auto-gen that often. I wish Linq designer can take non-column properties in the future.

There are 2 surprises, SP doesn’t need to change ‘set nocount off‘ and revise datediff which I had to do when using NHiberate.


4 thoughts on “Compare Linq to SQL with NHibernate

  1. Is it only properties specified in the model that can be used as stored procedure parameters or does it pick up additional properties of the class as well? I’ve been using an additional file which, following your convention above, would be named XXX.cs and in there I’ve defined partial classes with additional properties to augment the auto-generated ones in XXX.designer.cs.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s