fix(sql): change query execution to use ToList before FirstOrDefault

- Change from FirstOrDefaultAsync to ToListAsync + FirstOrDefault
- Ensures proper query execution for both EF6 (.NET Framework 4.8) and EF Core (.NET 8.0)
- Prevents potential query execution issues
- Add using System.Linq directive
This commit is contained in:
2026-08-04 16:34:04 +02:00
parent 111281ac08
commit 5b865c0442

View File

@@ -1,6 +1,5 @@
#if NET48 #if NET48
using System.Data.Entity; using System.Data.Entity;
using System.Linq;
#else #else
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
#endif #endif
@@ -8,6 +7,7 @@ using ECMJobRunner.Application.Common.Interfaces;
using ECMJobRunner.Infrastructure.Data; using ECMJobRunner.Infrastructure.Data;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Linq;
namespace ECMJobRunner.Infrastructure.Services namespace ECMJobRunner.Infrastructure.Services
{ {
@@ -41,16 +41,16 @@ namespace ECMJobRunner.Infrastructure.Services
// Entity Framework 6 implementation // Entity Framework 6 implementation
var result = await _context.Database var result = await _context.Database
.SqlQuery<TResult>(sql) .SqlQuery<TResult>(sql)
.FirstOrDefaultAsync(cancellationToken); .ToListAsync(cancellationToken);
return result; return result.FirstOrDefault();
#else #else
// Entity Framework Core implementation // Entity Framework Core implementation
var result = await _context.Database var result = await _context.Database
.SqlQueryRaw<TResult>(sql) .SqlQueryRaw<TResult>(sql)
.FirstOrDefaultAsync(cancellationToken); .ToListAsync(cancellationToken);
return result; return result.FirstOrDefault();
#endif #endif
} }
} }