Skip to content

Configuring DbContext

The EFCore module will read values from IConfiguration and populate the DbContextOptions<TDbContext> for you. This is what provides the connection string, database provider to use, etc.

The configuration may be applied by default, to all DbContexts and/or you may configure each one of them manually.

Most our services will have a single DbContext, however some Suite Modules you import may require its own persistence, hence configuring the defaults instead of each context is recommended.

Default Configuration

The most common way to provide configuration is using the appsettings.json file, however since the values come from IConfiguration any source may be used.

JSON
1
2
3
4
5
{
    "ITsynch.Suite.App.Modules.EntityFrameworkCoreModuleOptions": {
        "DefaultDatabaseProviderKey": "SqlServer"
    }
}

Configuring a ProviderKey is a requirement for the EFCore module to know which Database Provider to use.

The list of valid providers can be found on the ITsynch.Suite.Persistence.DatabaseProvider class. Currently it contains the following providers:

  1. SqlServer
  2. Sqlite
  3. Oracle (Not implemented yet)

Per DbContext Configuration

Each DbContext can be configured manually as well by using the ContextOptions:

JSON
{
    "ITsynch.Suite.App.Modules.EntityFrameworkCoreModuleOptions": {
        "DefaultDatabaseProviderKey": "SqlServer",
        "ContextOptions": {
            "MyDbContextClassName": {
                "DatabaseProviderKey": "Sqlite"
            }
        }
    }
}

The ContextOptions is a Dictionary of DbContext class name to its options.

Important

ContextOptions overrides the Default Options provided at the EFCore module level.