Entity Framework 6 connection string dll app confi

2019-07-11 18:54发布

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

2条回答
ゆ 、 Hurt°
2楼-- · 2019-07-11 19:09

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.

查看更多
Anthone
3楼-- · 2019-07-11 19:24

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.
查看更多
登录 后发表回答