Galin Iliev's blog

Software Architecture & Development

LINQ to SQL, Web.config and Connection Strings

I had some interesting adventures and in one of them I had to figure out the connection string key for LINQ to SQL DataContext class. I found this interesting blog post by Rick Strahl.

In short he describes that LINQ to SQL designer uses different approaches for web site project and class library one. Here is like the website project designer file is:

public DataClassesDataContext() :
        base(global::System.Configuration.ConfigurationManager.ConnectionStrings["TimeTrackerConnectionString"].ConnectionString, mappingSource)
{
    OnCreated();
}

while the class library one is:

public TimeTrakkerContext() :
 base(global::TimeTracker.Properties.Settings.Default.TimeTrackerConnectionString, mappingSource)
{
    OnCreated();
}

and you should overwrite it by adding connection string in web.config with name TimeTracker.Properties.Settings.TimeTrackerConnectionString like this

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="TimeTracker.Properties.Settings.TimeTrackerConnectionString"
            connectionString="Data Source=.;Initial Catalog=TimeTracker;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

Hmm it works with me too but I remember I had one record in my web.config like this TimeTracker.Properties.Settings.Default.TimeTrackerConnectionString and it worked till I made some change. Go find...

Comments (1) -

  • Damon

    7/31/2008 4:14:59 PM | Reply

    Very nice! Was this not also posted at linqhelp.com in 101 section?

Loading