diff --git a/BTCPayServer.Data/Data/APIKeyData.cs b/BTCPayServer.Data/Data/APIKeyData.cs index 129611e84..85e54502e 100644 --- a/BTCPayServer.Data/Data/APIKeyData.cs +++ b/BTCPayServer.Data/Data/APIKeyData.cs @@ -5,6 +5,7 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace BTCPayServer.Data { @@ -27,6 +28,22 @@ namespace BTCPayServer.Data public StoreData StoreData { get; set; } public ApplicationUser User { get; set; } public string Label { get; set; } + + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasOne(o => o.StoreData) + .WithMany(i => i.APIKeys) + .HasForeignKey(i => i.StoreId).OnDelete(DeleteBehavior.Cascade); + + builder.Entity() + .HasOne(o => o.User) + .WithMany(i => i.APIKeys) + .HasForeignKey(i => i.UserId).OnDelete(DeleteBehavior.Cascade); + + builder.Entity() + .HasIndex(o => o.StoreId); + } } public class APIKeyBlob diff --git a/BTCPayServer.Data/Data/AddressInvoiceData.cs b/BTCPayServer.Data/Data/AddressInvoiceData.cs index 4ae68a567..c6681b089 100644 --- a/BTCPayServer.Data/Data/AddressInvoiceData.cs +++ b/BTCPayServer.Data/Data/AddressInvoiceData.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace BTCPayServer.Data { @@ -32,5 +33,15 @@ namespace BTCPayServer.Data get; set; } + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasOne(o => o.InvoiceData) + .WithMany(i => i.AddressInvoices).OnDelete(DeleteBehavior.Cascade); + builder.Entity() +#pragma warning disable CS0618 + .HasKey(o => o.Address); +#pragma warning restore CS0618 + } } } diff --git a/BTCPayServer.Data/Data/AppData.cs b/BTCPayServer.Data/Data/AppData.cs index 705a74008..7754c95f9 100644 --- a/BTCPayServer.Data/Data/AppData.cs +++ b/BTCPayServer.Data/Data/AppData.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; namespace BTCPayServer.Data @@ -37,5 +38,14 @@ namespace BTCPayServer.Data { Settings = value == null ? null : JsonConvert.SerializeObject(value); } + + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasOne(o => o.StoreData) + .WithMany(i => i.Apps).OnDelete(DeleteBehavior.Cascade); + builder.Entity() + .HasOne(a => a.StoreData); + } } } diff --git a/BTCPayServer.Data/Data/ApplicationDbContext.cs b/BTCPayServer.Data/Data/ApplicationDbContext.cs index 86e030a08..c8c11e437 100644 --- a/BTCPayServer.Data/Data/ApplicationDbContext.cs +++ b/BTCPayServer.Data/Data/ApplicationDbContext.cs @@ -74,135 +74,19 @@ namespace BTCPayServer.Data { base.OnModelCreating(builder); NotificationData.OnModelCreating(builder); - - - builder.Entity() - .HasOne(o => o.StoreData) - .WithMany(a => a.Invoices).OnDelete(DeleteBehavior.Cascade); - builder.Entity().HasIndex(o => o.StoreDataId); - - - builder.Entity() - .HasOne(o => o.InvoiceData) - .WithMany(i => i.Payments).OnDelete(DeleteBehavior.Cascade); - builder.Entity() - .HasIndex(o => o.InvoiceDataId); - - builder.Entity() - .HasOne(o => o.StoreData) - .WithMany(i => i.UserStores).OnDelete(DeleteBehavior.Cascade); - builder.Entity() - .HasKey(t => new - { - t.ApplicationUserId, - t.StoreDataId - }); - - builder.Entity() - .HasOne(o => o.StoreData) - .WithMany(i => i.APIKeys) - .HasForeignKey(i => i.StoreId).OnDelete(DeleteBehavior.Cascade); - - builder.Entity() - .HasOne(o => o.User) - .WithMany(i => i.APIKeys) - .HasForeignKey(i => i.UserId).OnDelete(DeleteBehavior.Cascade); - - builder.Entity() - .HasIndex(o => o.StoreId); - - builder.Entity() - .HasOne(o => o.StoreData) - .WithMany(i => i.Apps).OnDelete(DeleteBehavior.Cascade); - builder.Entity() - .HasOne(a => a.StoreData); - - builder.Entity() - .HasOne(pt => pt.ApplicationUser) - .WithMany(p => p.UserStores) - .HasForeignKey(pt => pt.ApplicationUserId); - - builder.Entity() - .HasOne(pt => pt.StoreData) - .WithMany(t => t.UserStores) - .HasForeignKey(pt => pt.StoreDataId); - - - builder.Entity() - .HasOne(o => o.InvoiceData) - .WithMany(i => i.AddressInvoices).OnDelete(DeleteBehavior.Cascade); - builder.Entity() -#pragma warning disable CS0618 - .HasKey(o => o.Address); -#pragma warning restore CS0618 - - builder.Entity() - .HasKey(o => o.Id); - - builder.Entity() - .HasOne(o => o.InvoiceData) - .WithMany(o => o.PendingInvoices) - .HasForeignKey(o => o.Id).OnDelete(DeleteBehavior.Cascade); - - - builder.Entity() - .HasOne(o => o.StoreData) - .WithMany(i => i.PairedSINs).OnDelete(DeleteBehavior.Cascade); - builder.Entity(b => - { - b.HasIndex(o => o.SIN); - b.HasIndex(o => o.StoreDataId); - }); - - builder.Entity() - .HasOne(o => o.InvoiceData) - .WithMany(i => i.HistoricalAddressInvoices).OnDelete(DeleteBehavior.Cascade); - builder.Entity() - .HasKey(o => new - { - o.InvoiceDataId, -#pragma warning disable CS0618 - o.Address -#pragma warning restore CS0618 - }); - - - builder.Entity() - .HasOne(o => o.InvoiceData) - .WithMany(i => i.Events).OnDelete(DeleteBehavior.Cascade); - builder.Entity() - .HasKey(o => new - { - o.InvoiceDataId, -#pragma warning disable CS0618 - o.UniqueId -#pragma warning restore CS0618 - }); - - - builder.Entity() - .HasOne(o => o.StoreData) - .WithMany(i => i.PaymentRequests) - .OnDelete(DeleteBehavior.Cascade); - builder.Entity() - .Property(e => e.Created) - .HasDefaultValue(new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)); - - builder.Entity() - .HasIndex(o => o.Status); - - builder.Entity() - .HasKey(o => new - { - o.WalletDataId, -#pragma warning disable CS0618 - o.TransactionId -#pragma warning restore CS0618 - }); - builder.Entity() - .HasOne(o => o.WalletData) - .WithMany(w => w.WalletTransactions).OnDelete(DeleteBehavior.Cascade); - + InvoiceData.OnModelCreating(builder); + PaymentData.OnModelCreating(builder); + Data.UserStore.OnModelCreating(builder); + APIKeyData.OnModelCreating(builder); + AppData.OnModelCreating(builder); + AddressInvoiceData.OnModelCreating(builder); + PairingCodeData.OnModelCreating(builder); + PendingInvoiceData.OnModelCreating(builder); + Data.PairedSINData.OnModelCreating(builder); + HistoricalAddressInvoiceData.OnModelCreating(builder); + InvoiceEventData.OnModelCreating(builder); + PaymentRequestData.OnModelCreating(builder); + WalletTransactionData.OnModelCreating(builder); PullPaymentData.OnModelCreating(builder); PayoutData.OnModelCreating(builder); RefundData.OnModelCreating(builder); diff --git a/BTCPayServer.Data/Data/HistoricalAddressInvoiceData.cs b/BTCPayServer.Data/Data/HistoricalAddressInvoiceData.cs index 9bcc2fbdc..69a88b4f6 100644 --- a/BTCPayServer.Data/Data/HistoricalAddressInvoiceData.cs +++ b/BTCPayServer.Data/Data/HistoricalAddressInvoiceData.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace BTCPayServer.Data { @@ -40,5 +41,20 @@ namespace BTCPayServer.Data { get; set; } + + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasOne(o => o.InvoiceData) + .WithMany(i => i.HistoricalAddressInvoices).OnDelete(DeleteBehavior.Cascade); + builder.Entity() + .HasKey(o => new + { + o.InvoiceDataId, +#pragma warning disable CS0618 + o.Address +#pragma warning restore CS0618 + }); + } } } diff --git a/BTCPayServer.Data/Data/InvoiceData.cs b/BTCPayServer.Data/Data/InvoiceData.cs index ac6644bd9..97715db49 100644 --- a/BTCPayServer.Data/Data/InvoiceData.cs +++ b/BTCPayServer.Data/Data/InvoiceData.cs @@ -83,6 +83,10 @@ namespace BTCPayServer.Data public RefundData CurrentRefund { get; set; } internal static void OnModelCreating(ModelBuilder builder) { + builder.Entity() + .HasOne(o => o.StoreData) + .WithMany(a => a.Invoices).OnDelete(DeleteBehavior.Cascade); + builder.Entity().HasIndex(o => o.StoreDataId); builder.Entity() .HasOne(o => o.CurrentRefund); } diff --git a/BTCPayServer.Data/Data/InvoiceEventData.cs b/BTCPayServer.Data/Data/InvoiceEventData.cs index 7e5471e6e..09d39e5e2 100644 --- a/BTCPayServer.Data/Data/InvoiceEventData.cs +++ b/BTCPayServer.Data/Data/InvoiceEventData.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace BTCPayServer.Data { @@ -22,5 +23,20 @@ namespace BTCPayServer.Data } public string Message { get; set; } + + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasOne(o => o.InvoiceData) + .WithMany(i => i.Events).OnDelete(DeleteBehavior.Cascade); + builder.Entity() + .HasKey(o => new + { + o.InvoiceDataId, +#pragma warning disable CS0618 + o.UniqueId +#pragma warning restore CS0618 + }); + } } } diff --git a/BTCPayServer.Data/Data/PairedSINData.cs b/BTCPayServer.Data/Data/PairedSINData.cs index 396cd5275..7ed062922 100644 --- a/BTCPayServer.Data/Data/PairedSINData.cs +++ b/BTCPayServer.Data/Data/PairedSINData.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace BTCPayServer.Data { @@ -33,5 +34,17 @@ namespace BTCPayServer.Data { get; set; } + + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasOne(o => o.StoreData) + .WithMany(i => i.PairedSINs).OnDelete(DeleteBehavior.Cascade); + builder.Entity(b => + { + b.HasIndex(o => o.SIN); + b.HasIndex(o => o.StoreDataId); + }); + } } } diff --git a/BTCPayServer.Data/Data/PairingCodeData.cs b/BTCPayServer.Data/Data/PairingCodeData.cs index cf1226e69..36df41022 100644 --- a/BTCPayServer.Data/Data/PairingCodeData.cs +++ b/BTCPayServer.Data/Data/PairingCodeData.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace BTCPayServer.Data { @@ -46,5 +47,11 @@ namespace BTCPayServer.Data get; set; } + + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasKey(o => o.Id); + } } } diff --git a/BTCPayServer.Data/Data/PaymentData.cs b/BTCPayServer.Data/Data/PaymentData.cs index 2361ba097..c204c1b45 100644 --- a/BTCPayServer.Data/Data/PaymentData.cs +++ b/BTCPayServer.Data/Data/PaymentData.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace BTCPayServer.Data { @@ -29,5 +30,14 @@ namespace BTCPayServer.Data { get; set; } + + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasOne(o => o.InvoiceData) + .WithMany(i => i.Payments).OnDelete(DeleteBehavior.Cascade); + builder.Entity() + .HasIndex(o => o.InvoiceDataId); + } } } diff --git a/BTCPayServer.Data/Data/PaymentRequestData.cs b/BTCPayServer.Data/Data/PaymentRequestData.cs index ca3513eb2..5868cb282 100644 --- a/BTCPayServer.Data/Data/PaymentRequestData.cs +++ b/BTCPayServer.Data/Data/PaymentRequestData.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Text; using BTCPayServer.Client.Models; +using Microsoft.EntityFrameworkCore; namespace BTCPayServer.Data { @@ -20,6 +21,18 @@ namespace BTCPayServer.Data public Client.Models.PaymentRequestData.PaymentRequestStatus Status { get; set; } public byte[] Blob { get; set; } - + + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasOne(o => o.StoreData) + .WithMany(i => i.PaymentRequests) + .OnDelete(DeleteBehavior.Cascade); + builder.Entity() + .Property(e => e.Created) + .HasDefaultValue(new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero)); + builder.Entity() + .HasIndex(o => o.Status); + } } } diff --git a/BTCPayServer.Data/Data/PendingInvoiceData.cs b/BTCPayServer.Data/Data/PendingInvoiceData.cs index 2bfdde074..3b65cf1fe 100644 --- a/BTCPayServer.Data/Data/PendingInvoiceData.cs +++ b/BTCPayServer.Data/Data/PendingInvoiceData.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace BTCPayServer.Data { @@ -12,5 +13,13 @@ namespace BTCPayServer.Data get; set; } public InvoiceData InvoiceData { get; set; } + + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasOne(o => o.InvoiceData) + .WithMany(o => o.PendingInvoices) + .HasForeignKey(o => o.Id).OnDelete(DeleteBehavior.Cascade); + } } } diff --git a/BTCPayServer.Data/Data/UserStore.cs b/BTCPayServer.Data/Data/UserStore.cs index 89e6fab7d..8bb2aa10f 100644 --- a/BTCPayServer.Data/Data/UserStore.cs +++ b/BTCPayServer.Data/Data/UserStore.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; namespace BTCPayServer.Data { @@ -29,5 +30,27 @@ namespace BTCPayServer.Data get; set; } + + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasOne(o => o.StoreData) + .WithMany(i => i.UserStores).OnDelete(DeleteBehavior.Cascade); + builder.Entity() + .HasKey(t => new + { + t.ApplicationUserId, + t.StoreDataId + }); + builder.Entity() + .HasOne(pt => pt.ApplicationUser) + .WithMany(p => p.UserStores) + .HasForeignKey(pt => pt.ApplicationUserId); + + builder.Entity() + .HasOne(pt => pt.StoreData) + .WithMany(t => t.UserStores) + .HasForeignKey(pt => pt.StoreDataId); + } } } diff --git a/BTCPayServer.Data/Data/WalletTransactionData.cs b/BTCPayServer.Data/Data/WalletTransactionData.cs index bbfbe32ee..829c6318a 100644 --- a/BTCPayServer.Data/Data/WalletTransactionData.cs +++ b/BTCPayServer.Data/Data/WalletTransactionData.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; namespace BTCPayServer.Data @@ -13,6 +14,20 @@ namespace BTCPayServer.Data public string TransactionId { get; set; } public string Labels { get; set; } public byte[] Blob { get; set; } + internal static void OnModelCreating(ModelBuilder builder) + { + builder.Entity() + .HasKey(o => new + { + o.WalletDataId, +#pragma warning disable CS0618 + o.TransactionId +#pragma warning restore CS0618 + }); + builder.Entity() + .HasOne(o => o.WalletData) + .WithMany(w => w.WalletTransactions).OnDelete(DeleteBehavior.Cascade); + } } public class WalletTransactionInfo