Fix date conversion issue with Sqlite provider
This commit is contained in:
parent
22ce7e2e81
commit
54ecc39e1d
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using Elsa.Persistence.EntityFramework.Core.Extensions;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NodaTime;
|
||||
|
|
@ -8,6 +9,7 @@ namespace Elsa.Persistence.EntityFramework.Core.Configuration
|
|||
public static class ValueConverters
|
||||
{
|
||||
public static readonly ValueConverter<Instant, DateTimeOffset> InstantConverter = new(x => x.ToDateTimeOffset(), x => Instant.FromDateTimeOffset(x));
|
||||
public static readonly ValueConverter<Instant, DateTime> SqliteInstantConverter = new(x => x.ToDateTimeUtc(), x => Instant.FromDateTimeUtc(x.WithKind(DateTimeKind.Utc)));
|
||||
public static readonly ValueConverter<JObject?, string> JObjectConverter = new(x => x!.ToString(), x => JObject.Parse(x));
|
||||
}
|
||||
}
|
||||
|
|
@ -1,5 +1,8 @@
|
|||
using Elsa.Models;
|
||||
using System.Linq;
|
||||
using Elsa.Models;
|
||||
using Elsa.Persistence.EntityFramework.Core.Configuration;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NodaTime;
|
||||
|
||||
namespace Elsa.Persistence.EntityFramework.Core
|
||||
{
|
||||
|
|
@ -17,6 +20,23 @@ namespace Elsa.Persistence.EntityFramework.Core
|
|||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.ApplyConfigurationsFromAssembly(typeof(ElsaContext).Assembly);
|
||||
|
||||
if (Database.ProviderName == "Microsoft.EntityFrameworkCore.Sqlite")
|
||||
{
|
||||
// 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(Instant) || p.PropertyType == typeof(Instant?));
|
||||
foreach (var property in properties)
|
||||
{
|
||||
modelBuilder
|
||||
.Entity(entityType.Name)
|
||||
.Property(property.Name)
|
||||
.HasConversion(ValueConverters.SqliteInstantConverter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
using System;
|
||||
|
||||
namespace Elsa.Persistence.EntityFramework.Core.Extensions
|
||||
{
|
||||
public static class DateTimeExtensions
|
||||
{
|
||||
public static DateTime WithKind(this DateTime dateTime, DateTimeKind kind) => new(dateTime.Ticks, kind);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue