feat: SQLExecutor-Klasse für die Ausführung von Roh-SQL-Abfragen mit Entity Framework Core hinzufügen
This commit is contained in:
32
EnvelopeGenerator.Infrastructure/SQLExecutor.cs
Normal file
32
EnvelopeGenerator.Infrastructure/SQLExecutor.cs
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user