Skip to content

Untie LINQ to SQL Connection String from Application Settings

Did you know that by default LINQ to SQL defines your connection string in more than one place if you define your LINQ to SQL classes out from a web app, such as a data access layer class library?

  1. Under “Application Settings” stored as Settings.settings and compiled in with your code rendering it unchangeable without recompilation and making it very easy to be inspected upon via reflection.
  2. Stored in Web.config / App.config as a reference copy from “Application Settings” in making it very disconcerting as to the true location it is picked up from.
  3. Hard coded in the “.dbml” file of the <Connection /> node as clear text.


Three places?! For standalone web applications it’s not so bad, as it turns out it is still stored in the Web.config and hard coded to the .dbml file, but the connection string is changeable on the fly and it does not store any strings which are baked in.

However if you wish to setup a layered n-tier architecture with your LINQ to SQL classes residing in a separate project, you will suffer the compilation “gotcha”.  Point 1 was a particular problem for me as I needed to update the connection string after compilation with an NAnt build script for deployment to a testing server.  This obviously wasn’t acceptable, as it did not honour changes easily between different database configuration across multiple environments (dev, test, production) so I needed another solution.

Thankfully there was one, and it will shorten that list down (you will still be stuck with a local “.dbml” conn. string copy which appears to pose no deployment implication as it simply tells the LINQ to SQL designer which DB you are working with, which is good).

Remember the below instructions are if you have a separate project holding your LINQ to SQL classes or you your connection string is being compiled into application settings (potentially anything but a Web application?) What we want to do is first turn off application wide settings effectively freeing it from LINQ to SQL: -

  1. Open up the LINQ to SQL designer, and open the Properties tab of the designer (the schema itself), expand Connection and set Application Settings to False. Save.  Don’t you feel better already?
  2. Close that down and open up your DataContext designer file (dbml_name.designer.cs) and alter the DataContext constructor.  You will immediately notice how your connection string decided to jump in here as you turned off application wide settings.  So the part to focus on here is altering the base() inheritor. Renaming “MyConnString” below to suit your own. I also noticed a DatabaseAttribute on the class which I don’t think plays a big part and has any implications on the connection settings. You will also need a reference to System.Configuration:
    public dbDataContext() :
         base(ConfigurationManager.ConnectionStrings["MyConnString"].ConnectionString, mappingSource)
    
  3. Open the App.config or Web.config featured in the project where your LINQ to SQL classes reside, and rename the connection string to what you defined as “MyConnString“.
  4. You now must Cut the entire <connectionStrings /> entry with name change and Paste it into either the App.config or Web.config of the application which is to access the data, such as a web application, Silverlight, WPF, WCF etc.  It is important that you alter the configuration file of the calling application which is to access the data, as the ConfigurationManager defined in your LINQ to SQL classes will look for the .config file from where the calling application is executing from, no matter where your LINQ to SQL classes have been defined.  As you can see, it works a little differently from before.
  5. Now Right Click and open the Properties on your DAL or project containing your LINQ to SQL classes and remove the connection string “Application Setting” reference on the Settings tab.
  6. Rebuild.  You’re all done, now just do a Find in Files check for perhaps your database name that you know was featured in the connection string to check for any stragglers, there shouldn’t be any.


Now you can alter your configuration of App.config / Web.config at your discretion without fear that the connection string is embedded somewhere nasty and won’t be picked up!

VN:F [1.8.3_1051]
Rating: 5.0/5 (15 votes cast)
VN:F [1.8.3_1051]
Rating: +9 (from 9 votes)
Untie LINQ to SQL Connection String from Application Settings5.0515
Bookmark and Share
kick it on DotNetKicks.com
Shout it

NOW, FOR A WORD FROM OUR SPONSORS

18 Comments

  1. André Meneses

    What if i want to access the connectionString from more than one project?
    How can i acess the config file of Project2 from Project1?

    UN:F [1.8.3_1051]
    Rating: 3.0/5 (1 vote cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 09-Apr-09 at 9:51 pm | Permalink
  2. bhatty

    exactly the same issue i faced. thanks for the resolution!

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 21-Apr-09 at 5:33 am | Permalink
  3. Graham O'Neale

    @bhatty, no problem! Glad to help, it was a really annoying problem!

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 21-Apr-09 at 9:01 am | Permalink
  4. jon

    hey I tried your solution but as soon as I manually change the DBML in the designer any of the edits you mention are erased.

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 24-Apr-09 at 9:09 am | Permalink
  5. jon

    Instead I used a partial calss to the datac context:

    namespace Campbells.Infrastructure.DAC
    {
    public partial class userDataContext : System.Data.Linq.DataContext
    {
    public userDataContext()
    : base(ConfigurationManager.ConnectionStrings["Cam"].ConnectionString, mappingSource)
    {
    OnCreated();
    }
    }
    }

    UN:F [1.8.3_1051]
    Rating: 5.0/5 (1 vote cast)
    UN:F [1.8.3_1051]
    Rating: +1 (from 1 vote)
    Posted on 24-Apr-09 at 9:18 am | Permalink
  6. Graham O'Neale

    @jon: and you don’t receive “Member with the same signature already declared”? I think you would as your other declaration would be:

    public DB() : base("Data Source=..........", mappingSource)

    both taking string and MappingSource.

    and I imagine if you have removed the auto-gen’d statement from your .designer.cs file, then you would be back to square one as you were saying if you manually adjust the .dbml file.

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 24-Apr-09 at 10:16 am | Permalink
  7. jon

    If you remove all the connection gook then there is no default parameterless constructor for the data context generated. The partial method implements the default empty constructor.

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 24-Apr-09 at 11:10 am | Permalink
  8. jon

    Forgot to say. thanks for your help!

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 24-Apr-09 at 11:10 am | Permalink
  9. Graham O'Neale

    Yes but what I am saying is if you remove it (the default generated one), allowing your new partial with new constructor to be invoked, it will fail when the old constructor is re-build next generation.

    No probs :)

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 24-Apr-09 at 11:36 am | Permalink
  10. jon

    Hard to talk about this stuff eg? No I dont modify the designer.cs cant do that else you lose edits if you switch to design mode. But “connection gook: is not a technical term. what I implement was I followed your advice and removed that stuff from the properties of the data context and set the “useappsettings” to false. then the generated code did not have the parameterless constructor that my partial class declares. (it compiles and runs) and I can change my DC in design mode………

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 24-Apr-09 at 12:28 pm | Permalink
  11. I was struggling with this problem and thanks to both Graham and jon I not only have a solution that works but using the partial class approach one that is elegant too. Many thanks. :grin:

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 03-Jun-09 at 8:40 am | Permalink
  12. Splendid solution, thanks alot for posting it! :)

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 16-Sep-09 at 9:22 am | Permalink
  13. randomcat

    Hi,
    I’m working in VB.NET and the properties window will not let me get rid of the connection string. How can I implement Jon’s solution?
    Thanks :)
    R

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 21-Oct-09 at 3:42 am | Permalink
  14. randomcat

    I’ve used the constructor with the connection string as follows, solving my problem:

    Dim ctx As WhateverDBDataContext
    ctx = new WhateverDBDataContext(ConfigurationManager.AppSettings(“whateverDBConnectionString”))

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 21-Oct-09 at 4:16 am | Permalink
  15. Graham O'Neale

    Cool dude :)

    UA:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UA:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 21-Oct-09 at 4:20 am | Permalink
  16. I have been looking for a good solution for a couple of days (as i am new to this). This post took me through step by step. Thanks so much.

    UN:F [1.8.3_1051]
    Rating: 5.0/5 (1 vote cast)
    UN:F [1.8.3_1051]
    Rating: +1 (from 1 vote)
    Posted on 03-Dec-09 at 11:20 am | Permalink
  17. Leon

    This is very useful. I try this and hit you back for results.

    Thanks

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 17-Jan-10 at 11:07 pm | Permalink
  18. Leon

    When i change the datacontext read from appsettings. the designer.cs disappears.

    UN:F [1.8.3_1051]
    Rating: 0.0/5 (0 votes cast)
    UN:F [1.8.3_1051]
    Rating: 0 (from 0 votes)
    Posted on 17-Jan-10 at 11:43 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*
My name is Graham O'Neale and I'm a software architect from Gold Coast, Australia. I am an overtime thinker, full time coder and awake part time in the real world. I have a keen interest in software development, particularly in the realm of programming (C#, ASP.NET, ASP.NET MVC, LINQ (2 SQL), Entity Framework, Silverlight, Blend, WCF, WPF) and a keen interest in the cutting edge and innovation. I have a new found love for design patterns, ALT.NET practices and well crafted software architecture. The purpose of this blog is to express any thoughts, findings, tips and gripes along my travels in the wonderful world of coding and technology...