visual studio 2010 - How do I make a LINQ to SQL database connection fully configurable / generic? -


for asp.net c# app, i'd use linq sql query sql database, have defined connection string in web.config file. used wizard in visual studio 2010 create linq classes. however, upon inspection, .dbml file explicitly contains database name , connection string i'm using (in dev system), , .designer.cs file explicitly names database.

for example, in .dbml file:

   <?xml version="1.0" encoding="utf-8"?> <database name="mydatabasename" class="mylinqdatacontext" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007"> <connection mode="websettings" connectionstring="xxx" settingsobjectname="system.configuration.configurationmanager.connectionstrings" settingspropertyname="myconnectionstring" provider="system.data.sqlclient" /> 

and in .designer.cs file:

[global::system.data.linq.mapping.databaseattribute(name="mydatabasename")] 

although table structures static, don't know connection string , database name customers choose ahead of time, , i'd them have change web.config file in order configure database connection.

how set don't have compile or release static dev settings release build of product, allowing customers configure in 1 place?

thanks.

check explanation

you can initialize data context connection string fetched config file. overrides default connection string used in designer file.

another way creating own partial datacontext class , adding oncreated partial method gets called in default constructor. here implementation reading connectionstring web.config connectionstring section.

  partial class mydatacontext     {         partial void oncreated()         {             this.connection.connectionstring =               configurationmanager.connectionstrings["myconnection"].connectionstring;         }     } 

Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -