feat: SQLExecutor-Klasse für die Ausführung von Roh-SQL-Abfragen mit Entity Framework Core hinzufügen

This commit is contained in:
Developer 02
2025-04-29 16:26:26 +02:00
parent 33048e185b
commit 3b4ad2960a
3 changed files with 71 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
using EnvelopeGenerator.Application.Contracts.SQLExecutor.cs;
using Microsoft.EntityFrameworkCore;
namespace EnvelopeGenerator.Infrastructure;
public sealed class SQLExecutor<T> : ISQLExecutor<T> where T : class
{
private readonly EGDbContext _context;
public SQLExecutor(EGDbContext context)
{
_context = context;
}
public async Task<T?> ExecuteFirstAsync(string sql, CancellationToken cancellation = default, params object[] parameters)
=> await _context
.Set<T>()
.FromSqlRaw(sql, parameters)
.FirstOrDefaultAsync(cancellation);
public async Task<T?> ExecuteSingleAsync(string sql, CancellationToken cancellation = default, params object[] parameters)
=> await _context
.Set<T>()
.FromSqlRaw(sql, parameters)
.SingleOrDefaultAsync(cancellation);
public async Task<IEnumerable<T>> ExecuteAllAsync(string sql, CancellationToken cancellation = default, params object[] parameters)
=> await _context
.Set<T>()
.FromSqlRaw(sql, parameters)
.ToListAsync(cancellation);
}