I am developing MVC application. I have two projects in my application. one is MVC application contains, Controller and Views and the second one is DataLayer project.
I am confused about the where to write the connection string, because while publishing the application it takes the web.config file and I get the data from DataLayer project so should I add connection string in the app.config/Web.config of Data layer project ?
Also, would like to know what is the purpose and difference between app.config and web.config ?
Connection strings can be stored as key/value pairs in the connectionStrings section of the configuration element of an application configuration file. Child elements include add, clear, and remove.
Connection strings in configuration files are typically stored inside the <connectionStrings> element in the app. config for a Windows application, or the web. config file for an ASP.NET application.
<add name="yourconnectinstringName" connectionString="Data Source= DatabaseServerName; Integrated Security=true;Initial Catalog= YourDatabaseName; uid=YourUserName; Password=yourpassword; " providerName="System. Data. SqlClient" />
Every project comes with a configuration file when it's created. A general class library has a generic one called app.config
. A web project is more specific, so its file is called web.config
and comes with web-specific parameters. They both serve the same purpose.
The problem you are facing is that only the executable project's config file (web.config) is deployed by default. You have a few options:
System.Configuration.ConfigurationManager
. This alleviates you from passing the data to your data layer, but creates a strong dependency (your data layer will not work without a properly formatted web.config file). System.Configuration.ConfigurationManager
. I prefer this approach, as it seems like a good balance. You have a custom strongly-typed config section in the "default" config file.This related question has some good info too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With