0

I'm using EF in a solution which compiles into a .DLL (connection string is in the app.config files in that solution).

Whenever I tried to include the generated dll in another solution I found no other way than to put in the same connection string again.

So my question here is IS there any way to make it so that the connection string doesn't have to be recreated in a project that includes a dll (which uses EF and has a connection string defined in the app.config)?

4
  • Do you mean you'd like to provide a default if no connection string is specified in the executing assembly's config file? Or, do you expect that the entry in the referenced assembly's config file will somehow be used by the executing assembly? Dec 20, 2014 at 20:38
  • Exactly the question is if it is doable in a way so that I dont need to specify the connection string again in the executing assembly (in essence that dll shall be used by programmed modules and the programmers who will use the dll in their solutions should not have to worry about that connection string at all)
    – Thomas
    Dec 20, 2014 at 20:51
  • That's what I assumed; please see my answer regarding Settings files. I think the approach will meet your needs and provide some flexibility should you need to override the connection string for whatever reason. Dec 20, 2014 at 21:08
  • Yepp saw it tnx. It sounds quite nice. On monday one of my coworkers will see if that solution for the project (will accept an answer as soon as I get the info back from them that the answer worked for the project and the situation there [can't test from home so waiting for monday there])
    – Thomas
    Dec 20, 2014 at 21:19

2 Answers 2

1

If you're looking to provide a default connection string for use by a referenced assembly which will normally not change (for whatever reason) but which can be overridden by the config file of the executing assembly, you could use a Settings file in the referenced assembly.

A Settings file supports connection string settings and also embeds in the assembly a default value provided at compile-time. This way, it isn't necessary to provide a connection string in the .config file of the executing assembly unless you need to override the default value.

When you need the connection string, you reference the automatically generated settings class which corresponds to your settings file:

MySettingsFile.Default.MyConnectionString

It will use the default value provided at compile-time unless a value has been provided in the executing assembly's .config file. This can be used, for example, to pass the connection string to the constructor of your Entity Framework DbContext class. See the referenced assembly's app.config file for an example of how to override the connection string in the .config file when necessary.

Note that your application really only has a single .config file, and any .config file that is part of a referenced assembly's project will not be used (or even seen) by the executing assembly.

Please also note that using a Settings file to retrieve the connection string will not include the Provider Name, which Entity Framework would otherwise look at when given a connection name to determine which database engine is used. In the absence of a Provider Name, I believe the default will be SqlServer.

0

The point of the app.config file is to allow changes to the configuration of the app outside of the app itself. If you want to make your connection string non-configurable put it into another resource file and embed this file into your application or just add it as a constant in some global configuration class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.