I have a library for the data access layer where it uses Entity Framework, in the DbContext
class I pass the connection string name DefaultConnection
through the constructor, then I add this connection string to the app.config
file for this library.
But when I update to the database I find the database is not created into this connection string server and catalogue?
Anybody please explain how migration and update database handle connection strings?
Your library can't access the app.config
file in run time. Only executable components can (a web api, a console application, etc.). You have to define the connectionString
value in the config file of the executable component that uses your library, not in the library's app.config
file. The only scenario when the library's app.config
file connection string can be used is when running Entity Framework commands in the Package Manager console (like add-migration
and similar ones).
The recommended way to deal with connection strings is that the data layer library doesn't care at all about the responsibility of obtaining the connection string value. Instead, the client that uses the library should provide the connection string when instantiating the data layer classes. The responsibility of getting the value should be in that client component, using code like this to get the value to be passed to the data layer:
var connectionString = Configuration.GetConnectionString("MyConnectionString");
A common practice is to use a dependency injection component (like Autofac
) so that the client injects the connection string as a parameter in the DbContext
constructor, or in a property, after getting it from the configuration file (or possibly from other place).
There are also several approaches about what parts of your data layer you expose to the client components that use it: you can directly create instances of the DbContext
, or use the Repository
pattern, or the UnitOfWork
pattern. The important concept here is that whenever your client component creates an instance of one of your data layer classes, it has to get the connection string value from wherever it is and pass it to the data layer component (via directly invoking the constructor and passing the connection string value as a parameter, or setting a property, or via your dependency injection component).
Update: added some info about execution from Package Manager console:
To pass the connection string tothe Package Manager console you can do several things:
Add an explicit parameter ConnectionString
to your console commands (the complete connection string, not only the name in the config file). This avoids the need to add the connection string to your library's config file.
Add an explicit parameter ConnectionStringName
to your console commands. Define that connection string in the library app.config
(but be careful and don't forget that it will only be used from the console, and that the one used in run time is the one in the executable component config file. This approach may lead to some mistakes).
Add a parameterless constructor to your DbContext
with the connection string name hard-coded in the base constructor call, like this:
public MyDbContext() : base("DefaultConnection") { ... }
This will be the one that console manager will use. For real code execution use the other constructor with the parameter. Again, the connection string must be defined in the library's app.config
file and this may lead to mistakes.
Also, console manager uses by default the configuration from the project selected as start project in your solution. If this project is not the database library it will not work like you want. To fix this you should set your solution as "Multiple start up projects", and select the database layer project in the console manager "Default project" drop down list (or pass a DefaultProject
parameter with the data layer project name to your console commands).