Wednesday, October 22, 2008

Configurable connection string with Linq to SQL

When using Linq-to-SQL with the dbml designer, by default it generates an App.config file and puts the connection string to use in it. This is fine (well, not really, but at least it works) for applications, but when working with class libraries it's a problem – DLL's don't load the app.config, so changing the connection string in the app.config won't produce any change (the application will still try to use the connection string that was used at design time).

I found a pretty neat solution here by David Klein ( http://ddkonline.blogspot.com/2008/02/set-connection-string-in-linq-dbml-file.html ), which is based on Jon Gallant's solution ( http://blogs.msdn.com/jongallant/archive/2007/11/25/linq-and-web-application-connection-strings.aspx )

P.S: The problem is known by MSFT: http://msdn.microsoft.com/en-us/library/bb386996.aspx . It doesn't seems to bother them though…