1

I'm confused on how to do what the title says.

Basically I have two connection strings in my config file, a production one, and a development one.

I have a simple class library project that is basically my data access layer, that I want to unit test. I don't want to run all the unit tests on my production database so I would like it to use a different connection string.

On startup and teardown of my tests project it will create the necessary tables and temporary data in my development database, while with my real project I would like it to use the production one.

In my Data Access layer class library do I just expose the DataContext or some method to switch modes (debug, production)? Then the only way I can think to change the connection string is through the DataContext.Connection.ConnectionString property...but I'm not sure if that is correct.

Any insight would on this would be great.

Kyle Gobel
  • 5,530
  • 9
  • 45
  • 68

2 Answers2

1

If you are developing a web based solution you can use web.config transformation to obtain different .config files for debug and production.

If you are creating a desktop solution then it's more difficult but there are work arounds available. See this SO article: App.Config Transformation for projects which are not Web Projects in Visual Studio 2010?

Community
  • 1
  • 1
Phil
  • 42,255
  • 9
  • 100
  • 100
1

When creating a new instance of the datacontext, you can pass along the connection string you intend to use with it:

MyDataContext context = new MyDataContext(connectionString);
Trax72
  • 958
  • 5
  • 12