Dynamically set database name in LINQ to SQL

I am using LINQ to SQL to connect to the database from my application. When I change the environment from production to stage, I can update the connection string in web.config. But there is another value that I need to update when the environment changes. This is the name of the database. In the LINQ to SQL constructor file, the database name is referred to as an attribute -

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

How can I get the name value dynamically from some configuration file?

Any help really appreciated.

+5
source share
3 answers

-

public DataContext Context = new DataContext(SqlConnectionString); //much simplified
0

I fixed this problem by editing the .dbml file outside of Visual Studio (the constructor does not seem to allow access to the DatabaseAttribute attribute) and gets 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 overrides my connection string)

0
source

All Articles