5

I am using LINQ to SQL to connect to database from my application. When I am changing environment from production to staging, I can update my connection string in web.config. But there is one more value I need to update when environment changes. That is database name. In LINQ to SQL designer file, database name is mentioned as attribute like-

[System.Data.Linq.Mapping.DatabaseAttribute(Name="somedbname")]

How can I pick up value of Name dynamically from some config file?

Any help is really appreciated.

Manoj Attal
  • 2,806
  • 3
  • 28
  • 31
  • 1
    I found removing database attribute from dbml.cs file, did not give any error or exception. So this attribute is not compulsory, but added by default by designer. – Manoj Attal Sep 18 '09 at 08:52
  • Can you unaccept the accepted answer? It is no longer correct. – NH. Nov 20 '17 at 18:45

3 Answers3

5

as mentioned on the http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.databaseattribute.name.aspx

"The DatabaseName is used only if the connection itself does not specify the database name." so you can delete this attribute and all is gonna work fine!

0

I've used a wrapper class to provide a context along the lines of

public DataContext Context = new DataContext(SqlConnectionString); //much simplified
wefwfwefwe
  • 3,382
  • 1
  • 21
  • 24
0

I fixed this problem by editing the .dbml file outside of Visual Studio (the designer doesn't seem to allow access to the DatabaseAttribute) and getting rid of the name property here:

<Database Name="BadName" Class="OutputDataContext" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">

(note that the accepted answer is no longer correct: this attribute was overriding my connection string)

NH.
  • 2,240
  • 2
  • 23
  • 37