I'm creating a set of desktop applications (C#, .NET Framework 4.7), that all use the same database and models. I'm using Entity Framework 6 to setup the database models. To prevent having duplicate code (models), I placed all the models in a dedicated "database" class. In this class, I also define the DbContext.
When adding the EntityFramework package via NuGet to the "database" class, the EF database provider for SQLServer LocalDB was automatically installed as well.
Starting the application without further configuration would cause SQLServer LocalDB to initialize and be used for the database connection.
I have 2 concerns about the "database endpoint" configuration.
1) I want the actual application to set the database connection string. So every application, that may run on different machines, can have their own database connection string to point to the same database server.
To achieve this, I modified my current DbConext in the "database" class to the following:
public class MyDbContext : DbContext
{
public MyDbContext(string connectionString)
{
this.Database.Connection.ConnectionString = connectionString;
}
}
Now I can define the database connection string via the application and not the common "database" class.
2) I want to define the database engine at application level. So that I can create the "database" class as an "generic class" and define that actual EF database provider to be used also in the application.
How do I have to modify my common "database" class and the applications to let the application decide which EF database provider is to be used?