Two .NET projects, one DB connection string?

I have a .NET solution containing two projects:

  • ASP.NET MVC Project, 'Website'
  • Class Library, Models

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

The connection string is defined in the web.config file of the Web site 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 will have to update both projects.

Is there a way that I can centralize the connection string in one place and still use both projects?

+6
asp.net-mvc configuration connection-string
source share
9 answers

Create an incomplete class that matches your data context, and use this code to force the web.config line, not app.config. Place the partial class in the same place 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 information. Preferred method for connection string in class library
RichardOD posted a link to what I think best describes our LINQ To SQL problem and the value of WebStationString Web.Config

+4
source share

It's good. Place the model connection string in the Web.config file and forget (delete) the model configuration file.

The DataContext also contains a connection string in its constructor, so you can specify it as well (perhaps not on one line):

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

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

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

You may have made a mistake by not specifying the correct connection string in the Web.config file.

+2
source share

I would say that you must save the product configuration in the web.config web project, and then enter the configuration that you use in the model project.

+1
source share

Ok, I guess here, but it looks like both projects use the same connection string, right?

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

0
source share

I think the connection string in the model library is used by the designer. At run time, the string is loaded from web.config. Therefore, you do not have to sync strings.

0
source share

You do not need the app.config class library. Just enter your configuration in web.config. Also, if you need some kind of configuration section from the app.config library, just put it in the web.config file. The ConfigurationManager will read it from there.

0
source share

If your model class library is listed in your web application, you can simply delete the link in the App.Config file and make sure that you have something similar to what is hosted on your network. configurations. Thus, when the compiler looks at the web.config file, it will find the connection string that it needs for the MVC project, and therefore, no further look.

0
source share

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

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

Thus, my repository can be created, and I never think about setting up a connection or the need to deal with it. To change to / from dev / live databases, I just modify my web.config.

0
source share

The app.config file in the library will not affect production - these connection strings really exist for the linq2sql constructor. Nothing is visible, move, these are not the droids we are looking for.,.

0
source share

All Articles