简体   繁体   中英

web.config, app.config priority / dependancies and usage on compilation

Just a quick question. I have a solution splitted in multiple projects. In one project, I have the database interactions and my EDMX. In this project, I have my app.config file with some connections strings.

This project, is imported as dependancy in a Web project. In this one, I have my Web.config where are defined (or "overriden") connections strings.

I'd like to know what are the mechanisms used to configure the database connection. From what I understood, the Web.config has all priority over App.config. But what I'm wondering is, is the App.config in dependancies projects used at compilation time ?

For instance :

Project A => app.config :

<connectionStrings>
    <add name="A" connectionString="myConnectionStringA"/>
    <add name="B" connectionString="myConnectionStringB"/>
</connectionStrings>

And the same in Web.config but with :

<connectionStrings>
    <add name="A" connectionString="myConnectionStringC"/>
    <add name="B" connectionString="myConnectionStringD"/>
</connectionStrings>

Which one will be used to define the connection to the EDMX ? In one hand, at compile time, logically it would be A & B used to define it, and C & D would be used at runtime.

But i'm not sure about it and for me, once the dll is "configured", I don't see how can C and D be used instead of A & B.

Could someone explain it to me please ?

Thanks !

The config file that is used at runtime is the one related to where you are launching your application. If you launch the projectA it will be the App.config file.

Actually it will be the file generated by the compilation on the proper directory "Debug" or "Release"

When you run your web project it will be the Web.config file there.

The dll isn't "configured" with the values from the config file, they are read when the application starts running and this will depend on the application that is running. That is why if you change the values they will change when you relaunch the application without any need to recompile the project.

When designing your entities in Visual Studio, the connection string that is stored in the app.config file of the project is used.

Even though you add a reference to the project from the web project, the app.config of the referenced project is not used at all in the context of the web project. Of course, it can be used as a blueprint when adding the connection strings to the web.config.

The config file that is relevant to the web project is the web.config. So when running or publishing the web project, the settings that are used are the ones in the web.config. They do not override the settings of the app.config in the sense of a fallback like "if the connection string is not configured in the web.config file, then I use the ones stored in app.config". It is required that you add the connection strings that you want to use when running the web project to the web.config file, otherwise you'd encounter an error if you used the Entity classes.

For details on configuring ASP.NET web applications, see this link .

But i'm not sure about it and for me, once the dll is "configured", I don't see how can C and D be used instead of A & B.

The config values are fetched when the progam is running, not when it is compiled.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM