2

My solution has two projects. A class library to generate data from an Entity Framework .edmx file via a repository class. The App.Config file connectionString matches the Web.config in my second ASP.Net 4.0 web forms project.

I modify BOTH connectionStrings to point to the test database (DataSource=TestDB) when deployed to the test server. When deployed to the live site I modify both to point to the live database (DataSource=LiveDB).

At the top of Site.Master I display in red letters "You are connected to the TEST database" in a label control. I conditionally toggle the .Visible property of the label if (context.Connection.DataSource.Contains("TestDB"))

During development I noticed something strange. When Web.config points to TestDB, if I forget to make App.config match (and it points to LiveDB) the "connected to TEST" warning appears but data from LiveDB displays.

This tells me the repository (class library) uses App.Config but a data context created in the web forms project uses the Web.config connectionString. Is that correct? I'm concerned that live data will be updated incorrectly if I deploy to the test site and forget to point App.Config to TestDB.

So my question is, in this scenario... What is the best way to display a notification "connected to test database" that is always accurate?

2 Answers 2

4

By default a class library will not be using any app.config file.

It would use the .config file that is in the context of the application using it - in the case of a web application that would be the web.config file.

Sign up to request clarification or add additional context in comments.

12 Comments

But in my scenario this does not seem to be true. Why is live data displayed when Web.config points to test and App.Config points to live?
@DeveloperDan - Are you sure that you are not loading the configuration directly, using one of the Open*Configuration methods on ConfigurationManager?
I'm not using ConfigurationManager. The class library .edmx was built using the wizard and update model from database. In my web forms app I do: MyDataProject.MyEnities context = new MyDataProject.MyEnities() then use the if statement in the question.
When I change the connection string in App.Config the change is reflected in the .edmx properties (ConceptualEntityModel) and also in the data returned. That is the only place I've set up a connect string other than the initial EF Wizard which I believe created the initial App.Config connect string.
@ppumkin - there are defaults that get built into the generated classes. That might be what's biting you with EF.
|
0

When the app runs, it's using the one in web.config, no matter where the context is created.

Why not set up the firewall on your live application to prevent access from (a) anywhere except the live webserver, or (b) especially your test location?

3 Comments

But in my scenario this does not seem to be true. Why is live data displayed when Web.config points to test and App.Config points to live? I'll look into the setting up firewall.
The class library .edmx was built using the wizard and update model from database, so that is standard. In my web forms app I do: MyDataProject.MyEnities context = new MyDataProject.MyEnities() then use the if statement in the question. It seems non-esoteric to me.
Nope.... many times the app.config or conenction string gets baked into the damn DLL, i dont know why or how, but webconfig uses conn string A but the data is from Z

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.