2023-11-19 09:22:13 +00:00
|
|
|
|
using Elsa.EntityFrameworkCore.Common.Contracts;
|
|
|
|
|
|
using Elsa.EntityFrameworkCore.Extensions;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
|
|
|
|
|
|
2022-12-30 12:11:29 +00:00
|
|
|
|
namespace Elsa.EntityFrameworkCore.Common;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// An optional base class to implement with some opinions on certain converters to install for certain DB providers.
|
|
|
|
|
|
/// </summary>
|
2023-11-19 09:22:13 +00:00
|
|
|
|
public abstract class ElsaDbContextBase : DbContext, IElsaDbContextSchema
|
2022-05-26 10:47:31 +00:00
|
|
|
|
{
|
2023-03-07 20:40:53 +00:00
|
|
|
|
/// <summary>
|
2023-11-19 09:22:13 +00:00
|
|
|
|
/// The default schema used by Elsa.
|
2023-03-07 20:40:53 +00:00
|
|
|
|
/// </summary>
|
2023-05-28 14:40:10 +00:00
|
|
|
|
public static string ElsaSchema { get; set; } = "Elsa";
|
2023-11-19 09:22:13 +00:00
|
|
|
|
private string _schema;
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
|
|
public string Schema => _schema;
|
2023-03-07 20:40:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The table used to store the migrations history.
|
|
|
|
|
|
/// </summary>
|
2023-05-28 14:40:10 +00:00
|
|
|
|
public static string MigrationsHistoryTable { get; set; } = "__EFMigrationsHistory";
|
2022-05-26 10:47:31 +00:00
|
|
|
|
|
2023-03-07 20:40:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Initializes a new instance of the <see cref="ElsaDbContextBase"/> class.
|
|
|
|
|
|
/// </summary>
|
2022-11-20 18:00:03 +00:00
|
|
|
|
protected ElsaDbContextBase(DbContextOptions options) : base(options)
|
2022-05-26 10:47:31 +00:00
|
|
|
|
{
|
2023-11-19 09:22:13 +00:00
|
|
|
|
var elsaDbContextOptions = options.FindExtension<ElsaDbContextOptionsExtension>()?.Options;
|
|
|
|
|
|
|
|
|
|
|
|
// ReSharper disable once VirtualMemberCallInConstructor
|
|
|
|
|
|
_schema = !string.IsNullOrWhiteSpace(elsaDbContextOptions?.SchemaName) ? elsaDbContextOptions.SchemaName : ElsaSchema;
|
2022-05-26 10:47:31 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-07 20:40:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The schema used by Elsa.
|
|
|
|
|
|
/// </summary>
|
2023-11-19 09:22:13 +00:00
|
|
|
|
// protected virtual string Schema { get; set; }
|
2022-05-26 10:47:31 +00:00
|
|
|
|
|
2022-11-20 18:00:03 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-05-26 10:47:31 +00:00
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(Schema))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!Database.IsSqlite())
|
|
|
|
|
|
modelBuilder.HasDefaultSchema(Schema);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-04 15:04:14 +00:00
|
|
|
|
ApplyEntityConfigurations(modelBuilder);
|
2022-05-26 10:47:31 +00:00
|
|
|
|
|
|
|
|
|
|
if(Database.IsSqlite()) SetupForSqlite(modelBuilder);
|
|
|
|
|
|
if(Database.IsOracle()) SetupForOracle(modelBuilder);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-07 20:40:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Override this method to apply entity configurations.
|
|
|
|
|
|
/// </summary>
|
2022-09-04 15:04:14 +00:00
|
|
|
|
protected virtual void ApplyEntityConfigurations(ModelBuilder modelBuilder)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-07 20:40:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Override this method to apply entity configurations.
|
|
|
|
|
|
/// </summary>
|
2022-05-26 10:47:31 +00:00
|
|
|
|
protected virtual void Configure(ModelBuilder modelBuilder)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-07 20:40:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Override this method to apply entity configurations for the SQLite provider.
|
|
|
|
|
|
/// </summary>
|
2022-05-26 10:47:31 +00:00
|
|
|
|
protected virtual void SetupForSqlite(ModelBuilder modelBuilder)
|
|
|
|
|
|
{
|
|
|
|
|
|
// SQLite does not have proper support for DateTimeOffset via Entity Framework Core, see the limitations
|
|
|
|
|
|
// here: https://docs.microsoft.com/en-us/ef/core/providers/sqlite/limitations#query-limitations
|
|
|
|
|
|
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
|
|
|
|
|
|
{
|
|
|
|
|
|
var properties = entityType.ClrType.GetProperties().Where(p => p.PropertyType == typeof(DateTimeOffset) || p.PropertyType == typeof(DateTimeOffset?));
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var property in properties)
|
|
|
|
|
|
{
|
|
|
|
|
|
modelBuilder
|
|
|
|
|
|
.Entity(entityType.Name)
|
|
|
|
|
|
.Property(property.Name)
|
|
|
|
|
|
.HasConversion(new DateTimeOffsetToStringConverter());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-03-07 20:40:53 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Override this method to apply entity configurations for the Oracle provider.
|
|
|
|
|
|
/// </summary>
|
2022-05-26 10:47:31 +00:00
|
|
|
|
protected virtual void SetupForOracle(ModelBuilder modelBuilder)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|