diff --git a/BTCPayServer/BTCPayServer.csproj b/BTCPayServer/BTCPayServer.csproj
index 46bbe42c6..95bb96685 100644
--- a/BTCPayServer/BTCPayServer.csproj
+++ b/BTCPayServer/BTCPayServer.csproj
@@ -2,7 +2,7 @@
Exe
netcoreapp2.1
- 1.0.2.51
+ 1.0.2.52
NU1701,CA1816,CA1308,CA1810,CA2208
diff --git a/BTCPayServer/Services/Stores/StoreRepository.cs b/BTCPayServer/Services/Stores/StoreRepository.cs
index 8acdc066e..8bd4d4540 100644
--- a/BTCPayServer/Services/Stores/StoreRepository.cs
+++ b/BTCPayServer/Services/Stores/StoreRepository.cs
@@ -114,17 +114,34 @@ namespace BTCPayServer.Services.Stores
public async Task RemoveStoreUser(string storeId, string userId)
{
- bool delete = false;
using (var ctx = _ContextFactory.CreateContext())
{
var userStore = new UserStore() { StoreDataId = storeId, ApplicationUserId = userId };
ctx.UserStore.Add(userStore);
ctx.Entry(userStore).State = EntityState.Deleted;
await ctx.SaveChangesAsync();
- delete = await ctx.UserStore.Where(u => u.StoreDataId == storeId && u.Role == StoreRoles.Owner).CountAsync() == 0;
+
+ }
+ await DeleteStoreIfOrphan(storeId);
+ }
+
+ private async Task DeleteStoreIfOrphan(string storeId)
+ {
+ using (var ctx = _ContextFactory.CreateContext())
+ {
+ if (ctx.Database.SupportDropForeignKey())
+ {
+ if (await ctx.UserStore.Where(u => u.StoreDataId == storeId && u.Role == StoreRoles.Owner).CountAsync() == 0)
+ {
+ var store = await ctx.Stores.FindAsync(storeId);
+ if (store != null)
+ {
+ ctx.Stores.Remove(store);
+ await ctx.SaveChangesAsync();
+ }
+ }
+ }
}
- if (delete)
- await DeleteStore(storeId);
}
public async Task CreateStore(string ownerId, string name)
@@ -162,6 +179,7 @@ namespace BTCPayServer.Services.Stores
ctx.UserStore.Remove(storeUser);
await ctx.SaveChangesAsync();
}
+ await DeleteStoreIfOrphan(storeId);
}
public async Task UpdateStore(StoreData store)