2

I have 2 projects: Console Application and Class Library (dll)

My main project, the console app, references my dll.

In my dll I created an EF (Entity Framework) ADO model.

The EF connection string must be in the dll project .config file.

The problem is when I initialize a DbContext it is looking for the connection string in the Console Application .config file.

How can I tell the DbContext to look for the connection string in the dll .config file ?

Thanks

Howard
  • 81
  • 1
  • 4
  • The real question is _"Should I want to use dll config files?"_, to which the [answer is _"No"_](http://stackoverflow.com/questions/5674971/app-config-for-a-class-library). Try searching. – CodeCaster Jul 29 '15 at 16:29
  • Class libraries don't meaningfully *have* config files. Applications do. (You can *add* a config file to a class library, but a running application won't use it for anything.) – David Jul 29 '15 at 16:33

2 Answers2

3

That's how config files work. The running app always looks in {appname}.exe.config. The config file for the DLL project is just there to give you placeholders that must be copied to the running app's config file. It is not deployed along with the DLL.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Thanks but I don't want to be depended in the main project config file, I want my dll to work "out of the box", without setting the connection string in the main project app config. – Howard Jul 29 '15 at 16:33
  • 1
    You _can_ look for configurations in different places but that's determined by the _executable_ assembly. There's not a way that I'm aware of to deploy a DLL and a config file and the executable will automatically look for the DLL's config file. The point is that you're configuring the _application_ not the _library_. If you want to have the library look for it's configuration somewhere then config files will not work- you'll have to roll your own mechanism. – D Stanley Jul 29 '15 at 16:53
1

You should specify your connection string in your console app. Here is a quick example why you want to do that.

  • Imagine if you have another app that uses your DLL in your solution. What if it wants to connect to another database instance? Now you have to change your DLL connection string? But that would break your first app.
  • Very often you have different instances of you app running in different environments (e.g. development, staging, production). Each instance needs to connect to a different database. How would you reconfigure DLL connection string for each instance?
  • If somebody else uses your DLL, how can you know what database they want to point it to?

And there are a lot more arguments to why you really want to do it. All the tools are built around it too.

  • I believe Visual Studio doesn't add DLL config files when it compiles sources for apps that use these DLLs.
  • Hosting environments (e.g. Azure) allows you to easily change your web.config files, but don't know anything about your DLL config files.
Nikolai Samteladze
  • 7,699
  • 6
  • 44
  • 70
  • The connection string won't change, that's why I want it to be ad-hoc to my dll – Howard Jul 29 '15 at 16:33
  • @Howard: If the connection string won't change then don't make it a config setting. Hard-code it in the library. – David Jul 29 '15 at 16:36
  • @David How can I tell the 'DbContext' to look in that specific config file? – Howard Jul 29 '15 at 16:37
  • @Howard you don't need to change your connection string now. But what if you need to do that in the future? What if something happens with your DB server and you have to point you app to another one? – Nikolai Samteladze Jul 29 '15 at 16:39
  • @Howard: You don't, that's the point. If you're getting the connection string from the application config then it's going to look in *the application config*. But if you say that the connection string "won't change" then one option could be to hard-code it. It's unconventional and not generally recommended, but if you truly don't ever need to change the connection string then it's certainly an option. – David Jul 29 '15 at 16:41
  • Since I don't know what project is using my dll I can't specify in it's app.confg the connection string, it must be on my side, am I right? – Howard Jul 29 '15 at 16:43
  • @Howard: Alternatively you might create your own settings file for the class library. Rather than using the built-in application config code, you'd create your own class which reads from the file. (You can structure the file any way you like. XML, JSON, text, etc.) You'd just need to make sure that file gets published along with any application consuming the library. Which seems like considerably more work and more potential for errors than just using the application config. – David Jul 29 '15 at 16:45
  • @Howard: Why is it unreasonable to expect an application to be configured? What's specifically different about your situation that the developers consuming the class library shouldn't need to know about the config settings for that library? – David Jul 29 '15 at 16:46
  • @Howard you do specify your connection string in your DLL app.config (EF will add it there by default) and in your Console App app.config. – Nikolai Samteladze Jul 29 '15 at 16:47
  • @Howard it not only connection strings by the way. The app that consumes your DLL have to configure EF too. So you don't really win anything by having this one connection string in the DLL. – Nikolai Samteladze Jul 29 '15 at 16:51
  • @NikolaiSamteladze Why does my app have to configure the EF? If I want to have a DLL that wraps a database connection? – Howard Jul 29 '15 at 17:20
  • @Howard if your Console App references `DbContext` from your DLL or any of the generated entities, then it will need a reference to EF DLL. Its not a big deal of course. Just wanted to mention. – Nikolai Samteladze Jul 29 '15 at 18:15
  • Is there an option to specify a different .config file for 'DbContext' ? – Howard Jul 30 '15 at 07:30