tags:

views:

420

answers:

2

Whenever I drop a new database object from server explorer into the dbml, Visual Studio replaces the connection string in the designer file with a new one that it automatically adds to the connection strings in my config file. They both have exactly the same parameters, just different names.

How do I force Visual Studio to use and keep the connection string I already have?

For example, in the dbml-designer.cs, I have this:

base(global::System.Configuration.ConfigurationManager.ConnectionStrings["ConnString1"].ConnectionString, mappingSource)

which is automatically generated. In my config file, I have:

<add name="DefaultConnString" connectionString="EXACTLYTHESAME"
    providerName="System.Data.SqlClient" /> <add name="ConnString1" connectionString="EXACTLYTHESAME"
    providerName="System.Data.SqlClient" />

Where ConnString1 is added automatically whenever I drag a database object to the dbml.

I want the designer.cs to use DefaultConnstring.

+1  A: 

You'll need to make sure the connectionstring used in the Server Explorer of your Visual Studio is the same as the one already defined within the dbml file.

I'm not too sure what do you mean by different name, do you mean database name? When I have a different database name for development and production, what I normally do is I keep the development connectionstring value when developing, but change the connectionstring value in my main application. This is because I normally develop my LINQ logic in a separate assembly.

Alternatively, you keep two connectionstrings: one for adding new database object, the other for testing, as example. Of course, you'll then have to be extra careful in keeping track of which connectionstring you'll use.

alextansc
Thanks, edited my question so hopefully it's clearer what I meant.
Stefan
+3  A: 

There's a good post that pretty much explains your options here: http://blogs.msdn.com/jongallant/archive/2007/11/25/linq-and-web-application-connection-strings.aspx

Timothy Khouri
Thanks, exactly what I was looking for!
Stefan