From 5e15dd97b3b7caf698b9ad0881fd513704a62396 Mon Sep 17 00:00:00 2001 From: Kukks Date: Mon, 9 Mar 2020 09:44:44 +0100 Subject: [PATCH] fix sqlite again closes #1287 Seems like a merge conflict removed the fix for sqlite datetimeoffsets --- .../Data/ApplicationDbContext.cs | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/BTCPayServer.Data/Data/ApplicationDbContext.cs b/BTCPayServer.Data/Data/ApplicationDbContext.cs index b00c8e1ba..e1029a900 100644 --- a/BTCPayServer.Data/Data/ApplicationDbContext.cs +++ b/BTCPayServer.Data/Data/ApplicationDbContext.cs @@ -259,7 +259,27 @@ namespace BTCPayServer.Data builder.Entity() .HasOne(o => o.WalletData) .WithMany(w => w.WalletTransactions).OnDelete(DeleteBehavior.Cascade); - + + if (Database.IsSqlite() && !_designTime) + { + // 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 + // To work around this, when the Sqlite database provider is used, all model properties of type DateTimeOffset + // use the DateTimeOffsetToBinaryConverter + // Based on: https://github.com/aspnet/EntityFrameworkCore/issues/10784#issuecomment-415769754 + // This only supports millisecond precision, but should be sufficient for most use cases. + foreach (var entityType in builder.Model.GetEntityTypes()) + { + var properties = entityType.ClrType.GetProperties().Where(p => p.PropertyType == typeof(DateTimeOffset)); + foreach (var property in properties) + { + builder + .Entity(entityType.Name) + .Property(property.Name) + .HasConversion(new Microsoft.EntityFrameworkCore.Storage.ValueConversion.DateTimeOffsetToBinaryConverter()); + } + } + } } }