using System; using System.Linq; using System.Threading.Tasks; using BTCPayServer.Data.Data; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using Microsoft.EntityFrameworkCore.Infrastructure; namespace BTCPayServer.Data { public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory { public ApplicationDbContext CreateDbContext(string[] args) { var builder = new DbContextOptionsBuilder(); builder.UseSqlite("Data Source=temp.db"); return new ApplicationDbContext(builder.Options, true); } } public class ApplicationDbContext : IdentityDbContext { private readonly bool _designTime; public ApplicationDbContext(DbContextOptions options, bool designTime = false) : base(options) { _designTime = designTime; } #nullable enable public async Task GetMigrationState() { return (await Settings.FromSqlRaw("SELECT \"Id\", \"Value\" FROM \"Settings\" WHERE \"Id\"='MigrationData'").AsNoTracking().FirstOrDefaultAsync())?.Value; } #nullable restore public DbSet AddressInvoices { get; set; } public DbSet ApiKeys { get; set; } public DbSet Apps { get; set; } public DbSet CustodianAccount { get; set; } public DbSet Files { get; set; } public DbSet InvoiceEvents { get; set; } public DbSet InvoiceSearches { get; set; } public DbSet InvoiceWebhookDeliveries { get; set; } public DbSet Invoices { get; set; } public DbSet Notifications { get; set; } public DbSet OffchainTransactions { get; set; } public DbSet PairedSINData { get; set; } public DbSet PairingCodes { get; set; } public DbSet PayjoinLocks { get; set; } public DbSet PaymentRequests { get; set; } public DbSet Payments { get; set; } public DbSet Payouts { get; set; } public DbSet PendingInvoices { get; set; } public DbSet PlannedTransactions { get; set; } public DbSet PullPayments { get; set; } public DbSet Refunds { get; set; } public DbSet Settings { get; set; } public DbSet StoreSettings { get; set; } public DbSet StoreWebhooks { get; set; } public DbSet Stores { get; set; } public DbSet U2FDevices { get; set; } public DbSet Fido2Credentials { get; set; } public DbSet UserStore { get; set; } [Obsolete] public DbSet Wallets { get; set; } public DbSet WalletObjects { get; set; } public DbSet WalletObjectLinks { get; set; } [Obsolete] public DbSet WalletTransactions { get; set; } public DbSet WebhookDeliveries { get; set; } public DbSet Webhooks { get; set; } public DbSet LightningAddresses { get; set; } public DbSet PayoutProcessors { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { var isConfigured = optionsBuilder.Options.Extensions.OfType().Any(); if (!isConfigured) optionsBuilder.UseSqlite("Data Source=temp.db"); } protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); // some of the data models don't have OnModelCreating for now, commenting them ApplicationUser.OnModelCreating(builder); AddressInvoiceData.OnModelCreating(builder); APIKeyData.OnModelCreating(builder); AppData.OnModelCreating(builder); CustodianAccountData.OnModelCreating(builder); //StoredFile.OnModelCreating(builder); InvoiceEventData.OnModelCreating(builder); InvoiceSearchData.OnModelCreating(builder); InvoiceWebhookDeliveryData.OnModelCreating(builder); InvoiceData.OnModelCreating(builder); NotificationData.OnModelCreating(builder); //OffchainTransactionData.OnModelCreating(builder); BTCPayServer.Data.PairedSINData.OnModelCreating(builder); PairingCodeData.OnModelCreating(builder); //PayjoinLock.OnModelCreating(builder); PaymentRequestData.OnModelCreating(builder); PaymentData.OnModelCreating(builder); PayoutData.OnModelCreating(builder); PendingInvoiceData.OnModelCreating(builder); //PlannedTransaction.OnModelCreating(builder); PullPaymentData.OnModelCreating(builder); RefundData.OnModelCreating(builder); SettingData.OnModelCreating(builder, Database); StoreSettingData.OnModelCreating(builder, Database); StoreWebhookData.OnModelCreating(builder); StoreData.OnModelCreating(builder, Database); U2FDevice.OnModelCreating(builder); Fido2Credential.OnModelCreating(builder); BTCPayServer.Data.UserStore.OnModelCreating(builder); //WalletData.OnModelCreating(builder); WalletObjectData.OnModelCreating(builder, Database); WalletObjectLinkData.OnModelCreating(builder, Database); #pragma warning disable CS0612 // Type or member is obsolete WalletTransactionData.OnModelCreating(builder); #pragma warning restore CS0612 // Type or member is obsolete WebhookDeliveryData.OnModelCreating(builder); LightningAddressData.OnModelCreating(builder); PayoutProcessorData.OnModelCreating(builder); //WebhookData.OnModelCreating(builder); 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()); } } } } } }