Do not use AsAsyncEnumerable()

This commit is contained in:
nicolas.dorier 2019-10-04 22:55:11 +09:00
parent ccc2d0e13c
commit 68807bae37
No known key found for this signature in database
GPG key ID: 6618763EF09186FE
5 changed files with 28 additions and 35 deletions

View file

@ -7,10 +7,8 @@ using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
#if NETCOREAPP21
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
#endif
using System.Linq;
namespace BTCPayServer.Authentication
@ -39,9 +37,8 @@ namespace BTCPayServer.Authentication
return Array.Empty<BitTokenEntity>();
using (var ctx = _Factory.CreateContext())
{
return (await ctx.PairedSINData.AsAsyncEnumerable()
.AsAsyncEnumerable().Where(p => p.SIN == sin)
.ToListAsync())
return (await ctx.PairedSINData.Where(p => p.SIN == sin)
.ToArrayAsync())
.Select(p => CreateTokenEntity(p))
.ToArray();
}
@ -51,7 +48,7 @@ namespace BTCPayServer.Authentication
{
using (var ctx = _Factory.CreateContext())
{
return await ctx.ApiKeys.AsAsyncEnumerable().AsAsyncEnumerable().Where(o => o.Id == apiKey).Select(o => o.StoreId).FirstOrDefaultAsync();
return await ctx.ApiKeys.Where(o => o.Id == apiKey).Select(o => o.StoreId).FirstOrDefaultAsync();
}
}
@ -69,7 +66,7 @@ namespace BTCPayServer.Authentication
using (var ctx = _Factory.CreateContext())
{
var existing = await ctx.ApiKeys.AsAsyncEnumerable().Where(o => o.StoreId == storeId).FirstOrDefaultAsync();
var existing = await ctx.ApiKeys.Where(o => o.StoreId == storeId).FirstOrDefaultAsync();
if (existing != null)
{
ctx.ApiKeys.Remove(existing);
@ -83,7 +80,7 @@ namespace BTCPayServer.Authentication
{
using (var ctx = _Factory.CreateContext())
{
return await ctx.ApiKeys.AsAsyncEnumerable().AsAsyncEnumerable().Where(o => o.StoreId == storeId).Select(c => c.Id).ToArrayAsync();
return await ctx.ApiKeys.Where(o => o.StoreId == storeId).Select(c => c.Id).ToArrayAsync();
}
}
@ -171,7 +168,7 @@ namespace BTCPayServer.Authentication
ctx.PairingCodes.Remove(pairingCode);
// Can have concurrency issues... but no harm can be done
var alreadyUsed = await ctx.PairedSINData.AsAsyncEnumerable().Where(p => p.SIN == pairingCode.SIN && p.StoreDataId != pairingCode.StoreDataId).AnyAsync();
var alreadyUsed = await ctx.PairedSINData.Where(p => p.SIN == pairingCode.SIN && p.StoreDataId != pairingCode.StoreDataId).AnyAsync();
if (alreadyUsed)
return PairingResult.ReusedKey;
await ctx.PairedSINData.AddAsync(new PairedSINData()
@ -192,7 +189,7 @@ namespace BTCPayServer.Authentication
{
using (var ctx = _Factory.CreateContext())
{
return (await ctx.PairedSINData.AsAsyncEnumerable().Where(p => p.StoreDataId == storeId).ToListAsync())
return (await ctx.PairedSINData.Where(p => p.StoreDataId == storeId).ToListAsync())
.Select(c => CreateTokenEntity(c))
.ToArray();
}

View file

@ -40,12 +40,13 @@ namespace BTCPayServer
{
public static class Extensions
{
#if NETCOREAPP21
public static T AsAsyncEnumerable<T>(this T obj)
#if !NETCOREAPP21
public static IQueryable<TEntity> Where<TEntity>(this Microsoft.EntityFrameworkCore.DbSet<TEntity> obj, System.Linq.Expressions.Expression<Func<TEntity, bool>> predicate) where TEntity : class
{
return obj;
return System.Linq.Queryable.Where(obj, predicate);
}
#endif
public static string Truncate(this string value, int maxLength)
{
if (string.IsNullOrEmpty(value))

View file

@ -108,7 +108,7 @@ namespace BTCPayServer
bool save = false;
using (var ctx = _DBContextFactory.CreateContext())
{
foreach (var store in await ctx.Stores.ToArrayAsync())
foreach (var store in await ctx.Stores.AsQueryable().ToArrayAsync())
{
#pragma warning disable CS0618 // Type or member is obsolete
var blob = store.GetStoreBlob();
@ -136,7 +136,7 @@ namespace BTCPayServer
{
using (var ctx = _DBContextFactory.CreateContext())
{
foreach (var app in ctx.Apps.Where(a => a.AppType == "Crowdfund"))
foreach (var app in await ctx.Apps.Where(a => a.AppType == "Crowdfund").ToArrayAsync())
{
var settings = app.GetSettings<Services.Apps.CrowdfundSettings>();
#pragma warning disable CS0618 // Type or member is obsolete
@ -154,7 +154,7 @@ namespace BTCPayServer
{
using (var ctx = _DBContextFactory.CreateContext())
{
foreach (var store in await ctx.Stores.ToArrayAsync())
foreach (var store in await ctx.Stores.AsQueryable().ToArrayAsync())
{
var blob = store.GetStoreBlob();
#pragma warning disable CS0618 // Type or member is obsolete
@ -174,7 +174,7 @@ namespace BTCPayServer
{
using (var ctx = _DBContextFactory.CreateContext())
{
foreach (var store in await ctx.Stores.ToArrayAsync())
foreach (var store in await ctx.Stores.AsQueryable().ToArrayAsync())
{
var blob = store.GetStoreBlob();
#pragma warning disable CS0612 // Type or member is obsolete
@ -204,7 +204,7 @@ namespace BTCPayServer
{
using (var ctx = _DBContextFactory.CreateContext())
{
foreach (var store in await ctx.Stores.ToArrayAsync())
foreach (var store in await ctx.Stores.AsQueryable().ToArrayAsync())
{
foreach (var method in store.GetSupportedPaymentMethods(_NetworkProvider).OfType<Payments.Lightning.LightningSupportedPaymentMethod>())
{

View file

@ -8,9 +8,7 @@ using System.Linq;
using System.Threading.Tasks;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Migrations;
#if NETCOREAPP21
using Microsoft.EntityFrameworkCore;
#endif
namespace BTCPayServer.Services.Stores
{
@ -41,7 +39,7 @@ namespace BTCPayServer.Services.Stores
using (var ctx = _ContextFactory.CreateContext())
{
return (await ctx
.UserStore.AsAsyncEnumerable()
.UserStore
.Where(us => us.ApplicationUserId == userId && us.StoreDataId == storeId)
.Select(us => new
{
@ -71,7 +69,7 @@ namespace BTCPayServer.Services.Stores
using (var ctx = _ContextFactory.CreateContext())
{
return await ctx
.UserStore.AsAsyncEnumerable()
.UserStore
.Where(u => u.StoreDataId == storeId)
.Select(u => new StoreUser()
{
@ -86,7 +84,7 @@ namespace BTCPayServer.Services.Stores
{
using (var ctx = _ContextFactory.CreateContext())
{
return (await ctx.UserStore.AsAsyncEnumerable()
return (await ctx.UserStore
.Where(u => u.ApplicationUserId == userId)
.Select(u => new { u.StoreData, u.Role })
.ToArrayAsync())
@ -124,7 +122,7 @@ namespace BTCPayServer.Services.Stores
{
if (!ctx.Database.SupportDropForeignKey())
return;
foreach (var store in await ctx.Stores.AsAsyncEnumerable().Where(s => s.UserStores.Where(u => u.Role == StoreRoles.Owner).Count() == 0).ToArrayAsync())
foreach (var store in await ctx.Stores.Where(s => s.UserStores.Where(u => u.Role == StoreRoles.Owner).Count() == 0).ToArrayAsync())
{
ctx.Stores.Remove(store);
}
@ -151,7 +149,7 @@ namespace BTCPayServer.Services.Stores
{
if (ctx.Database.SupportDropForeignKey())
{
if (await ctx.UserStore.AsAsyncEnumerable().Where(u => u.StoreDataId == storeId && u.Role == StoreRoles.Owner).CountAsync() == 0)
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)
@ -195,7 +193,7 @@ namespace BTCPayServer.Services.Stores
{
using (var ctx = _ContextFactory.CreateContext())
{
var storeUser = await ctx.UserStore.AsAsyncEnumerable().FirstOrDefaultAsync(o => o.StoreDataId == storeId && o.ApplicationUserId == userId);
var storeUser = await ctx.UserStore.AsQueryable().FirstOrDefaultAsync(o => o.StoreDataId == storeId && o.ApplicationUserId == userId);
if (storeUser == null)
return;
ctx.UserStore.Remove(storeUser);

View file

@ -10,12 +10,8 @@ using Microsoft.AspNetCore.Identity;
using NBitcoin;
using U2F.Core.Models;
using U2F.Core.Utils;
#if NETCOREAPP21
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query;
#else
using Microsoft.Extensions;
#endif
namespace BTCPayServer.U2F
{
@ -39,7 +35,7 @@ namespace BTCPayServer.U2F
{
using (var context = _contextFactory.CreateContext())
{
return await context.U2FDevices.AsAsyncEnumerable()
return await context.U2FDevices
.Where(device => device.ApplicationUserId.Equals(userId, StringComparison.InvariantCulture))
.ToListAsync();
}
@ -64,7 +60,7 @@ namespace BTCPayServer.U2F
{
using (var context = _contextFactory.CreateContext())
{
return await context.U2FDevices.AsAsyncEnumerable().AnyAsync(fDevice => fDevice.ApplicationUserId.Equals(userId, StringComparison.InvariantCulture));
return await context.U2FDevices.AsQueryable().AnyAsync(fDevice => fDevice.ApplicationUserId.Equals(userId, StringComparison.InvariantCulture));
}
}
@ -149,9 +145,10 @@ namespace BTCPayServer.U2F
using (var context = _contextFactory.CreateContext())
{
var device = await context.U2FDevices.AsAsyncEnumerable().SingleOrDefaultAsync(fDevice =>
var keyHandle = authenticateResponse.KeyHandle.Base64StringToByteArray();
var device = await context.U2FDevices.Where(fDevice =>
fDevice.ApplicationUserId.Equals(userId, StringComparison.InvariantCulture) &&
fDevice.KeyHandle.SequenceEqual(authenticateResponse.KeyHandle.Base64StringToByteArray()));
fDevice.KeyHandle.SequenceEqual(keyHandle)).SingleOrDefaultAsync();
if (device == null)
return false;
@ -183,7 +180,7 @@ namespace BTCPayServer.U2F
{
using (var context = _contextFactory.CreateContext())
{
var devices = await context.U2FDevices.AsAsyncEnumerable().Where(fDevice =>
var devices = await context.U2FDevices.Where(fDevice =>
fDevice.ApplicationUserId.Equals(userId, StringComparison.InvariantCulture)).ToListAsync();
if (devices.Count == 0)