Class SqliteModelCustomizer
Overrides the default model creation process in the DbContext{T} with configurations suitable for SQLite.
Implements
Inherited Members
Namespace: Wolfgang.DbContextBuilderCore
Assembly: Wolfgang.DbContextBuilder-Core.dll
Syntax
public class SqliteModelCustomizer : ModelCustomizer, IModelCustomizer
Remarks
Unless the production database you are testing is also SQLite, there will be differences between your database and the context's configuration definition and SQLite's capabilities. This class provides some basic overrides to make your DbContext work in SQLite. This class provides some basic capabilities like,
- Renaming tables to avoid schema issues since SQLite does not support schemas.
- Removing computed values for columns since SQLite may not support the same functions.
You can override the functionality provided in this class or, if you will frequently work
against the same database engine, derive your own ModelCustomizer (from
scratch or from this one) and override the desired functionality. Once created it can be
reused across builds. For example, you could create a SqliteForOracleModelCustomizer
with the overrides needed to make an Oracle DbContext work against SQLite.
Constructors
| Edit this page View SourceSqliteModelCustomizer(ModelCustomizerDependencies)
Initializes a new instance of the SqliteModelCustomizer class.
Declaration
public SqliteModelCustomizer(ModelCustomizerDependencies dependencies)
Parameters
| Type | Name | Description |
|---|---|---|
| ModelCustomizerDependencies | dependencies | The dependencies for the model customizer. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException |
|
Properties
| Edit this page View SourceDefaultValueMap
A dictionary where the key is the default value as defined in the model and the value is the value to replace it with.
Declaration
public IDictionary<string, string> DefaultValueMap { get; }
Property Value
| Type | Description |
|---|---|
| IDictionary<string, string> |
Remarks
Examples of default values that may need to be replaced are getdate() and
newid(), which would be replaced with datetime('now') and
lower(hex(randomblob(16))) respectively. Key lookups are case-insensitive
(OrdinalIgnoreCase) so variant spellings produced by
different EF versions match the same replacement entry.
Configure this map during builder setup (before BuildAsync); mutating it
after the first Customize pass is unsupported because the customizer is
typically registered as a DI singleton and concurrent reads on the dictionary
during model build are not guarded.
OverrideComputedValueHandling
This method is called for each column that has a computed value defined in the model.
Declaration
public Func<string?, string?> OverrideComputedValueHandling { get; set; }
Property Value
| Type | Description |
|---|---|
| Func<string, string> |
Remarks
The default handling for this is to leave the computed value as is. However, Sqlite has limited support for computed values and the functions used in the model may be incompatible with SQLite. You can override this behavior by assigning a custom implementation to this property.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | The setter received a |
OverrideDefaultValueHandling
This method is called for each column that has a default value defined in the model.
Declaration
public Func<string?, string?> OverrideDefaultValueHandling { get; set; }
Property Value
| Type | Description |
|---|---|
| Func<string, string> |
Remarks
The default implementation checks the DefaultValueMap dictionary to see if the existing default value is contained in the dictionary and if so, replaces it with the value in the dictionary. If the current default is not in the dictionary, it is left unchanged. You can override this behavior by assigning a custom implementation to this property.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | The setter received a |
OverrideManyToManyTableHandling
This action is called for each entity type to handle many-to-many join table renaming.
Declaration
public Action<IMutableEntityType> OverrideManyToManyTableHandling { get; set; }
Property Value
| Type | Description |
|---|---|
| Action<IMutableEntityType> |
Remarks
The default implementation uses a heuristic to detect many-to-many join tables: an entity type with exactly two foreign keys and no navigations is assumed to be a join table. When detected, the table is renamed to "{LeftTable}_{RightTable}". You can override this behavior by assigning a custom implementation to this property.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | The setter received a |
OverrideTableRenaming
This method is called for each table in the database to rename the table since SQLite does not support schemas.
Declaration
public Func<(string? SchemaName, string TableName), string> OverrideTableRenaming { get; set; }
Property Value
| Type | Description |
|---|---|
| Func<(string SchemaName, string TableName), string> |
Remarks
The default implementation renames the table by prefixing the schema name to the table name. For example, a table named "Person" in schema Personnel would be renamed to "Personnel_Person". If the table doesn't have a schema "dbo" is used as the schema name. You can override this behavior by assigning a custom implementation to this property.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | The setter received a |
Methods
| Edit this page View SourceCustomize(ModelBuilder, DbContext)
Overrides the default model creation process in the DbContext{T} with configurations suitable for SQLite.
Declaration
public override void Customize(ModelBuilder modelBuilder, DbContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| ModelBuilder | modelBuilder | The builder being used to construct the model. |
| DbContext | context | The context instance that the model is being created for. |