using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; using ReC.Domain.QueryOutput; using ReC.Domain.Views; using System.Text.Json; namespace ReC.Application.Common.Interfaces; public interface IRecDbContext { #region DbSets public DbSet RecActionViews { get; set; } public DbSet ProfileViews { get; set; } public DbSet RecResultViews { get; set; } public DbSet HeaderQueryResults { get; set; } public DbSet BodyQueryResults { get; set; } public DbSet RecResults { get; set; } #endregion DbSets public Task SaveChangesAsync(CancellationToken cancel = default); public DatabaseFacade Database { get; } } public static class RecDbContextSaveExtensions { public static async Task ExecuteDynamicSqlAsync(this IRecDbContext context, string sql) { var result = new List>(); using var command = context.Database.GetDbConnection().CreateCommand(); command.CommandText = sql; await context.Database.OpenConnectionAsync(); using var reader = await command.ExecuteReaderAsync(); while (await reader.ReadAsync()) { var row = new Dictionary(); for (int i = 0; i < reader.FieldCount; i++) { var columnName = reader.GetName(i); var value = reader.IsDBNull(i) ? null : reader.GetValue(i); row[columnName] = value; } result.Add(row); } return JsonSerializer.Serialize(result); } }