Most Microsoft technologies that you can operate with a GUI come with some tradeoffs. Things have certainly improved over the years and now something like the LINQ to SQL designer is pretty trouble free – unless of course you have something like this fairly common scenario:
I had a class library (Data Access), and decided to add LINQ to SQL classes for a new database that was being introduced.
This class library is also ultimately being consumed by WCF web services. I have dev, test, prod environments, so I use ASP.NET Web Deployment projects to change configuration per environment for things like appSettings and connectionStrings.
It therefore followed that I wanted to configure the LINQ DataContext connection properties in web.config. Out of the box you’ll find your connection properties go into your Settings properties class, which gets a little bit in the way.
If you start playing around with the generated classes to change where you’re getting the connection info from then any changes in the designer will wipe them out, so a (relatively) pain free approach to setting your connection safely is the following:
Go to your LINQ to SQL designer and remove the Connection String, and set Application Settings to False
Create a new partial class to mirror your DataContext, and set the constructor to retrieve from your alternative source…
using System;
using System.Linq;
using System.Configuration;
using System.Data.Linq;
namespace CodeBureau.Services.DataAccess
{
public partial class MyDataContext : DataContext
{
public MyDataContext()
: base(ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString,
mappingSource)
{
OnCreated();
}
}
}
This will leave all your generated code intact, but will sort out your configuration woes.