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# | |
---|---|
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
:
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
.
The DefaultConnection
will be used in two scenarios:
- A connection string couldn't be found in
IConfiguration
for a connection string name that has been resolved for aDbContext
. - 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# | |
---|---|
JSON | |
---|---|
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 DbContext
s that do not.
JSON | |
---|---|
In this case, the DefaultConnection
will be used for
MyDbContext: SuiteDbContext<MyDbContext>
.