views:

635

answers:

9

I have a .NET solution containing two projects:

  • An ASP.NET MVC project, 'Website'
  • A class library, 'Models'

The 'Models' project contains a Linq-to-SQL data-context along with a few partial classes that extend the database objects.

The connection string is defined in the web.config of the 'Website' project.

However the 'Models' project seems to have its own app.config where the database connection is defined separately.

This means that if the connection string changes, I'll have to update both projects.

Is there a way I can centralize the connection string to one place, and still have both projects use it?

+1  A: 

I'd say that you should keep the configuration of the product in the web project's web.config, and then inject the configuration you use into the model project.

Thomas Lundström
This what I do. See my answer for actual code example
Breadtruck
+2  A: 

That's fine. Put the Model's connection string in the Web.config and forget (delete) the Model's config file.

The DataContext also takes in a connection string in its constructor so you can specify it this way too (perhaps not on one line though):

DataContext context = new DataContext(ConfigurationManager.ConnectionStrings["TheKey"].ConnectionString);

Edit- based on your comments to other answers it looks like you are doing something wrong. If you are doing that, then the generated code will use the default setting value.

System.Configuration.DefaultSettingValueAttribute("Data Source=SERVER;Initial Catalog=XYZ;Integrated Security=True")].

You might be making the mistake of not specifying the connection string properly in the Web.config file.

RichardOD
@RichardOD: I am commenting, but I didn't actually ask the question, although I did ask one similiar to this one a while back. I never tried to just delete the appconfig file. My aunt had suggested it, but I also had some dataset models in the project and in this case you have to create numerous partial tableadapter classes, and I wasn't going to do that, so I haven't tried to delete the app.config, but I imagine that it would work.
Breadtruck
A: 

Ok, I'm guessing here, but it looks like both projects use an identical connection string, yes?

In that case, only specify the connection string in the web.config; the class library app.config will not be read by ASP.NET anyway.

Randolpho
That has not been my experience unless I had something configured wrong.
Breadtruck
Really? Are you using a third party component that does it for you? Because in my experience only the app.config for the executable (or in the case of ASP.NET web.config) is read by the system by default. If there's an app.config associated with a class library (.DLL), the contents of that app.config must either be A) manually loaded (which is what third party components that do this for you do) or B) copied to the app.config/web.config of the main application.
Randolpho
@Randolpho: Please see http://stackoverflow.com/questions/1148559 for more clarification as it relates to me.
Breadtruck
@Randolpho: By the way I didn't downvote you, someone also downvoted me.
Breadtruck
A: 

I think the connection string in the Models library is just used by the designer. During runtime the string is loaded from the web.config. So you do not necessarily need to have the strings synchronised.

Stefan Egli
That is not what I have found after deploying a solution like this into production. The library continued to pull its connection from the app.config.
Breadtruck
+3  A: 

Create a partial class that is the same as your data context and use this code to force the usage of the web.config string as opposed to the app.config. Put the partial class in the same location as your model in the class library.

public partial class YourDataContext 
{
    partial void OnCreated()
    {
        ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings["PrimaryConnectionString"];

        if (cs != null)
        {
             this.Connection.ConnectionString = cs.ConnectionString;
        }
    }
}

See this question for more info Preferred Method for connection string in class library
RichardOD posted a link to what I think best describes our problem LINQ To SQL and the Web.Config ConnectionString Value

Breadtruck
What is the down vote for?
Breadtruck
But how will this work, since partial classes must belong in the same assembly? I want the 'Models' assembly to use the connection string from the web.config in the 'Website' assembly.
jonathanconway
BTW, I didn't do any downvoting.
jonathanconway
The code above will attempt to set the connection string to "PrimaryConnectionString" that it will find in the web.config because that is where the configuration manager will look. Otherwise if the value is null, the onCreated method will continue and use the default information found in the designer generated data context class. RichardOD posted a great link that discusses this whole topic. You can also try reading my similar question on this http://stackoverflow.com/questions/1148559
Breadtruck
I might also add that I called my app.config connection string something different than my web CS, "SecondaryConnectionString"
Breadtruck
Trust me it will work, because I am doing it just like that in my project. Put that partial class in the same location as the model and see!
Breadtruck
A: 

You don't need the class library app.config. Just put your configuration at the web.config. Also, if you need some configuration section from the library's app.config, just put it on the web.config. The ConfigurationManager will read it from there.

Fernando
A: 

If your Models Class library is referenced in your Web Application, you can simply remove the reference in the App.Config file and make sure you have something similar to that placed in your web. config. That way, when the compiler looks in the web.config, it will find the connection string it needs for the MVC project and hence have no need to look further down for it.

Kobojunkie
I tried this, but it seems to be getting the connection string from a 'Settings' file under the properties of the Models project. There's some code like this in the class: 'global::Models.Properties.Settings.Default.WebConnectionString, mappingSource)'
jonathanconway
@jonathanconroy : which is exactly why my answer will help you get around this problem.
Breadtruck
A: 

Personally, I extend DataContext in my Respository then do something like:

public ContractsControlRepository()
      : base(ConfigurationManager.ConnectionStrings["AccountsConnectionString"].ToString()) { }

That way my repository can be instantiated and I never worry about setting up a connection, or having to deal with it whatsoever. To change to/from dev/live databases I just change my web.config.

Kezzer
A: 

The app.config in the library won't effect production--those connection strings are really there for the linq2sql designer. Nothing to see, move along, these aren't the droids we are looking for . . .

Wyatt Barnett