9

With my source in Subversion, I am having issues when 2 different computers have different connection strings.

The LINQ to SQL designer seems to only like having the same connection string.

Is it possible for the designer to use a connection string that varies since developers have different local configurations, but the actual usage in the web application pulls from the web.config?

3 Answers 3

5

Unfortunately, this is a huge source of pain with the LINQ to SQL designer. I do not know of a way to force visual studio to never add a default connection string when you drag tables or stored procedures onto the design surface.

We work around the issue thusly:

  1. we never save our dev passwords
  2. we never use the default connection string in code when newing up a DataContext
  3. we can therefore "safely" ignore the multiple connection strings during sprints that touch the data layer
  4. when things die down/become more stable, we delete the connection strings from the data context, either using properties of the designer surface itself or by editing the XML. At that point, it's up to the modifier of the data context to keep up with deleting the default connection strings.

Alas, this is not an ideal situation. Hopefully VS2010 will "fix" this issue.

Sign up to request clarification or add additional context in comments.

3 Comments

VS2010 Beta 2 hasn't fixed this.
It seems VS2013 hasn't got this fixed either.
It seems VS2019 hasn't got this fixed either. =|
1

I ran into this problem and found your question. Here's the solution we're now using:

  1. Use a centralised Configuration class for retrieving config values from a particular location on the file system. This allows every machine where the code is running to use its own config values.

  2. Create a partial class for the LINQ to SQL data context. Add a custom constructor that takes no parameters and retrieves the database connection string from the Configuration class described above.

For example:

public partial class MyCustomDBDataContext
{
    public MyCustomDBDataContext() :
                base(Configuration.GetDatabaseConnectionString())
    {
    }
}

This should now solve the problem both for developers and when deployed to test and production.

2 Comments

Just trying to implement this for my project but im getting the error "Already defines a Method for..." because now it have 2 declarations of the default constructor.
I know this is an old comment but whatever...From an open and focused .dbml's "Properties" panel you need to expand "Connection" then set "Application Settings" to False. This will remove the parameterless constructor from the dbml's designer.cs which means your constructor in the partial class will no longer emit the "Already defined" error. The one downside is that if you delete/add a table to the dbml it will reset the connection string and set Application Setting back to True. Yes, it is very annoying.
1

By following the guidelines on How Do I? Change Connection String of datacontext default constructor from web.config my application now uses different connectionstrings depending on if HttpContext.Current.Request is local or not.

//using System.Web.Configuration;

partial void OnCreated()
    {
        //Change this condition to your needs
        var isLocal = HttpContext.Current.Request.IsLocal;
        this.Connection.ConnectionString = WebConfigurationManager.ConnectionStrings[isLocal ? "localConnectionstring" : "otherConnectionstring"].ToString();
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.