Skip to content

Connection Strings

The PersistenceModule supports defining multiple connection strings. Each DbContext can declare what connection string it binds to. By default, the schema name is used, which value is obtained from the [DefaultSchemaName] or the IEFCoreDatabaseContextOptions.DefaultSchemaName

C#
1
2
3
4
5
[DefaultSchemaName("MySchemaName")]
public class MyDbContext: SuiteDbContext<MyDbContext>
{
    // [..]
}

Important

By convention, the schema name should be written in PascalCase.

Once we have declared the schema name for our DbContext, we can provide the actual value from IConfiguration, by using the ConnectionStrings object. For example, using the appsettings.json:

JSON
1
2
3
4
5
{
    "ConnectionStrings": {
        "MySchemaName": "Data source=my-sqlite.db"
    }
}

Each DbContext should provide a schema name by using the [DefaultSchemaName] attribute. Hence, that means for our application we will have to configure multiple connection strings.

We could do that if we need to, that's the point of the granularity. However in most cases we don't want that, and that's where the DefaultConnection becomes important.

DefaultConnection

We can provide a special connection string named DefaultConnection that will be used when a connection string name cannot be resolved for a DbContext.

JSON
1
2
3
4
5
{
    "ConnectionStrings": {
        "DefaultConnection": "Data source=all-apps.db"
    }
}

The DefaultConnection will be used in two scenarios:

  1. A connection string couldn't be found in IConfiguration for a connection string name that has been resolved for a DbContext.
  2. A connection string name has not been resolved for a DbContext.

That means that we can use DefaultConnection to configure the connection string that all modules use.

Example 1

No connection string is provided for the name iInspector in IConfiguration.

C#
1
2
3
4
5
[DefaultSchemaName("iInspector")]
public class MyDbContext: SuiteDbContext<MyDbContext>
{
    // [..]
}
JSON
1
2
3
4
5
6
{
    "ConnectionStrings": {
        "DefaultConnection": "Data source=iinspector.db",
        "AMOS": "Data source=amos.db"
    }
}

Here, the connection string named iInspector could not be found in IConfiguration since there is no connection string named that way.

In this case, the DefaultConnection will be the connection string used for MyDbContext: SuiteDbContext<MyDbContext>, since a connection string named iInspector could not be found.

Example 2

When a DbContext has no [DefaultSchemaName] attribute, the DefaultConnection will be used.

Warning

It is recommended that DbContext always declare it's connection string name. PersistenceModule will raise a warning for DbContexts that do not.

C#
1
2
3
4
public class MyDbContext: SuiteDbContext<MyDbContext>
{
    // [..]
}
JSON
1
2
3
4
5
6
{
    "ConnectionStrings": {
        "DefaultConnection": "Data source=iinspector.db",
        "AMOS": "Data source=amos.db"
    }
}

In this case, the DefaultConnection will be used for MyDbContext: SuiteDbContext<MyDbContext>.