How to best change the database connection via the ContextManager

How to best change the database connection via the ContextManager

Old forum URL: forums.lhotka.net/forums/t/5903.aspx


raz0rf1sh posted on Sunday, November 30, 2008

We are currently making the switch to LinqToSql for our Dal layer.  Currently we assigned the application connection string to a modified BusinessPrincipal object, so that we can change our database connection on the fly.  For example, we have one user interface, and the user can select whether to work in the production or test environment.  Works great!

The problem is that the ContextManager seems to work off of the DataContext name.  So this is really limiting our flexibility, as we can't set the database connection string through the ContextManager as it assumes the Dbml file and the connection string have the same name.

Any ideas how to get around this?


sergeyb replied on Sunday, November 30, 2008

I think there is an overload in ContextManager that allows you to tell it to use specified value as connection string, not the connection string’s name.

 

Sergey Barskiy

Principal Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: raz0rf1sh [mailto:cslanet@lhotka.net]
Sent: Sunday, November 30, 2008 8:22 AM
To: Sergey Barskiy
Subject: [CSLA .NET] How to best change the database connection via the ContextManager

 

We are currently making the switch to LinqToSql for our Dal layer.  Currently we assigned the application connection string to a modified BusinessPrincipal object, so that we can change our database connection on the fly.  For example, we have one user interface, and the user can select whether to work in the production or test environment.  Works great!

The problem is that the ContextManager seems to work off of the DataContext name.  So this is really limiting our flexibility, as we can't set the database connection string through the ContextManager as it assumes the Dbml file and the connection string have the same name.

Any ideas how to get around this?




raz0rf1sh replied on Sunday, November 30, 2008

I took another look and there is an overloads that let's me pass in whether it's a database name or a connection string.

So I just do the following:

using (var mgr = ContextManager
.GetManager(Database.ApplicationConnection, false))

Curelom replied on Monday, December 01, 2008

I found that overload as well.  I would think the default should be toward a connection string and the overload should be for database name.

RockfordLhotka replied on Monday, December 01, 2008

Curelom:
I found that overload as well.  I would think the default should be toward a connection string and the overload should be for database name.

Maybe - but the best practice for .NET is to put the connection strings in the config file, and so the default overload is to enable that best practice.

Curelom replied on Monday, December 01, 2008

Ok, I'm feeling a little foolish now.  I've been using it like this

using (var mgr = Csla.Data.ContextManager<HD.Library.Data.DBDataContext>

.GetManager(ConfigurationManager.ConnectionStrings["HDConnectionString"], false))

 

but we can use it like this

 

using (var mgr = Csla.Data.ContextManager<HD.Library.Data.DBDataContext>

.GetManager("HDConnectionString"))

Copyright (c) Marimer LLC