I'm writing an application using Entity Framework and ASP.NET MVC, so I decided to separate DAL from MVC Web Application. And actually everything fine except next problem (if it's problem): I had to copy Entity connection string from app.config of DAL project to Web.config of Web Application. And actually it's look correct: DAL should not to know about connection string to Data, it's duty of Main Application.
But I really don't like, that I have to keep Entity connection string in app.config of DAL, because it's need to update my EntityModel (*.edmx). Is there some best practice to reference to connection string of Web Application? Or shoul I just leave it like it is now?
The web. config file is required for ASP.NET webpages. The app. config file is optional in an application and doesn't have to be used when writing desktop applications.
Web. Config is used for asp.net web projects / web services. App.Config is used for Windows Forms, Windows Services, Console Apps and WPF applications.
App. Config is an XML file that is used as a configuration file for your application. In other words, you store inside it any setting that you may want to change without having to change code (and recompiling). It is often used to store connection strings.
web. config file is an XML-based configuration file used in ASP. NET-based applications to manage various settings that are concerned with the configuration of our website. In this way, we can separate our application logic from configuration logic.
So, just an an explanation of what is occurring here. When you are developing your application, the DAL project is, by nature, it's own project. When editing the DAL project, Visual Studio will use the values in the projects configuration file. Thus, why the connection string staying in that app.config file.
Now, when you publish your entire solution to a web server, the web application is the actual application that is running. This has a reference to the DAL dll that was complied during the build process. Given this, the DAL dll will get all of its configuration values from the web.config as this is the configuration file for the primary executing assembly.
To answer your question, its probably best to keep them in both the app.config and web.config for no other reason than you will not have to 'create a new connection' each time you need to update your model. The DAL project, as far as I know, will not ever look to another projects configuration file while in Visual Studio. However, having connection strings in both the app.config and web.conig will not impact your deployment. The web.config settings will always be used.
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