linqdatasource runtime connection string

  • Thread starter Thread starter Chuck P
  • Start date Start date
C

Chuck P

I want to set the connection string to use at runtime for my linqsql
datacontext.
I am using the dbml or mapper in Visual Studio.
The way I created objects was by dragging objects from the server explorer
onto the design surface.

When I dragged the first object it made a connection string in my web.config
called:
XXX with integrated security.
I want the datacontext to use connection YYY which I have in the web.config.
If I change the Connection property in the dataContext object the next time
I add something to the designer it forces me to change the connection string
back to XXX.

My connection strings in the web.config get changed by MsBuild for test and
release.
I am really worried that someone will make a change to the DataContext
designer and the connection string will get set to XXX for use in Production.


Is their a good way to work around this?
 
Chuck

Why are you not setting it than in this case hardcoded in your datacontext
and remove the string from the config file.

(Not tried)

Cor
 
Thanks for Cor's reply.

Hello Chuck,

When we adds items to the LINQ to SQL Designer, all items use one shared
data connection. If you add an object to the designer that uses a data
connection that differs from the data connection currently being used by
the designer, VS IDE force you to use new connection string. Otherwise, the
selected object will not be added. I think this is because VS IDE needs to
use new connection to retrieve underling database, and generates code in
Designer.cs file.

If you didn't want to connect to production database, I'm afraid you have
to change connection back by yourself. Alterative way is to modify the
connection string in your web.config file directly.

Hope this helps, Please feel free to let us know if you have any more
concern. We are glad to assist you.
Have a great day,
Best regards,
Wen Yuan
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I would like to do that but you can't specify your datacontext connection
string when using a LinqDataSource.
 
Hello Chuck,
Thanks for your reply.

By default, LinqDataSource create data context by using default
constructor. If you want to use specific connection, I think you can create
data context object by yourself, and assign the object to the
ObjectInstance property in ContextCreating event.

Please refer to the following document. There is a simple about how to
specify connect string for LinqDataSource by yourself.
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatas
ource.contextcreating.aspx
[LinqDataSource..::.ContextCreating Event]

Hope this helps. Please feel free to let me know if you have any more
concern. We are glad to assist you.
Wen Yuan
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
I ended up searching on the Autogenerated connection name and changing it in
the designer.cs and the .dbml file.

Looks like that works, I could still drop tables and stuff from
ServerExplorer and have it work.
 
oops, that doesn't work.

Chuck P said:
I ended up searching on the Autogenerated connection name and changing it in
the designer.cs and the .dbml file.

Looks like that works, I could still drop tables and stuff from
ServerExplorer and have it work.


"Wen Yuan Wang [MSFT]" said:
Hello Chuck,
Thanks for your reply.

By default, LinqDataSource create data context by using default
constructor. If you want to use specific connection, I think you can create
data context object by yourself, and assign the object to the
ObjectInstance property in ContextCreating event.

Please refer to the following document. There is a simple about how to
specify connect string for LinqDataSource by yourself.
http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatas
ource.contextcreating.aspx
[LinqDataSource..::.ContextCreating Event]

Hope this helps. Please feel free to let me know if you have any more
concern. We are glad to assist you.
Wen Yuan
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
By default, LinqDataSource create data context by using default
constructor.

What connection gets used in the default constructor the one hardcoded into
the .dbml file or the one in the web.config?


If I look at the properties for the dbml in the designer their is a
Connection : Application Settings property. The help box says Specifies
whether connection information from the Application Settings File should be
used.

What is the Application Settings File, where is it?


If I change the values in the web.config of the connection string the
designer creates, will the new settings be used?
 
Hello Chuck,
Thanks for your reply.

Making change in designer.cs file doesn't help. VS IDE will re-create
designer.cs file after you modified .dbml.

The connection string gets used in default datacontext constructor varies
according to the connection property in dbml file in designer.
Application setting property in Connection indicates if the connection
string is stored in Application Settings or hard coded in DBML file.
- For Website application, the application setting is stored in web.config
file.
- For Winform/Library application, the application setting is stored in
app.config file.

VS IDE generates the default Datacontext constructor in designer.cs file as
below, if you specified "Application Setting" to "False".
public DataClasses1DataContext() :
base("Data Source=.\\;Initial Catalog=testDB;Integrated Security=True",
mappingSource)
{OnCreated();}

And, the following code is VS IDE generated when "Application Setting" is
"true".
public DataClassesDataContext() :

base(global::System.Configuration.ConfigurationManager.ConnectionStrings["te
stDBConnectionString"].ConnectionString, mappingSource)
{OnCreated();}

If you set the "Application Setting" property in Connection as "True", the
value you changed in web.config file of the connection string will take
effect.
On the other side, you have to modify the connection in DBML file directly,
if "Application Setting" is false.

Please feel free to let us know if you have any more concern or there is
anything unclear. We are glad to assist you.
Have a great day,
Best regards,
Wen Yuan

Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Back
Top