2

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?

3 Answers 3

1

EF supports this out of the box through app.config/web.config using the <connectionStrings> section.

In your application app.config, add:

Then in your DbContext class:

public class MyDbContext : DbContext
{
    public MyDbContext() 
        : base("ApplicationDatabase")
    { }

    public MyDbContext(string connectionString) 
        : base(connectionString)
    { }
}

This way each client instance can customize it's connection string at runtime by adjusting the "ApplicationDatabase" connection string in the MyApplication.exe.config file for Windows applications. The above code defaults to the "ApplicationDatabase" connection string if no connection string/setting is provided, or you can construct one with a given connection string or connection string setting.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming you are on Asp net Core.

This is what your startup.cs should look like.

       public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors();
            services.AddMvc();

            services.AddDbContext<MyDbContext>(op => op.UseSqlServer(Configuration["DbConnection"]));
       }

This is how you DbContext Constructor should look like.

public RipBookDbContext(DbContextOptions<RipBookDbContext> options) : base(options)
{

}

Your connectionstring will be saved in appSettings.json as

"DbConnection": "Data Source=.;Initial Catalog=DbName;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"

After this you can inject your DbContext class anywhere you want to use it.

4 Comments

we can not assume that we are on .net core when we are not
@Ghassen : Sameer wrote this answer previous to my edit in which I introduced the missing information about the environment. I'm sure, I can adapt from this. Have to play around a litte...
@burnersk yes you can do somethning like .net core logic but it was clear from the way we set the connection string that it is not .net core
@Sameer Kamran, this is a desktop application
0

You could expose an Interface for your DBContext. Then use a factory pattern to create the correct context using the proper provider. Microsoft has a good example here:

https://github.com/Microsoft/InventorySample/tree/master/src

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.