70 lines
2.6 KiB
C#
70 lines
2.6 KiB
C#
using Koogle.Domain.Entities;
|
|
using KoogleApp.Entities;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory.Database;
|
|
|
|
namespace Koogle.Infrastructure.Data
|
|
{
|
|
public class ApplicationDbContext : DbContext
|
|
{
|
|
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
|
|
: base(options)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Spieltage
|
|
/// </summary>
|
|
public DbSet<Day> Days => Set<Day>();
|
|
|
|
/// <summary>
|
|
/// Spieltage
|
|
/// </summary>
|
|
public DbSet<Player> Players => Set<Player>();
|
|
|
|
|
|
/// <summary>
|
|
/// Konfiguriert das Datenbankmodell.
|
|
/// </summary>
|
|
/// <param name="modelBuilder">Der ModelBuilder für die Konfiguration.</param>
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
base.OnModelCreating(modelBuilder);
|
|
|
|
// Alle IEntityTypeConfiguration-Klassen aus der aktuellen Assembly laden
|
|
modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
|
|
|
|
// Globale Query-Filter für Soft-Delete auf alle BaseEntity-Ableitungen anwenden
|
|
ApplySoftDeleteQueryFilters(modelBuilder);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Wendet globale Query-Filter für Soft-Delete auf alle Entitäten an,
|
|
/// die von BaseEntity erben.
|
|
/// </summary>
|
|
/// <param name="modelBuilder">Der ModelBuilder für die Konfiguration.</param>
|
|
private static void ApplySoftDeleteQueryFilters(ModelBuilder modelBuilder)
|
|
{
|
|
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
|
|
{
|
|
if (typeof(BaseEntity).IsAssignableFrom(entityType.ClrType))
|
|
{
|
|
var parameter = System.Linq.Expressions.Expression.Parameter(entityType.ClrType, "e");
|
|
var property = System.Linq.Expressions.Expression.Property(parameter, nameof(BaseEntity.IsDeleted));
|
|
var falseConstant = System.Linq.Expressions.Expression.Constant(false);
|
|
var comparison = System.Linq.Expressions.Expression.Equal(property, falseConstant);
|
|
var lambda = System.Linq.Expressions.Expression.Lambda(comparison, parameter);
|
|
|
|
modelBuilder.Entity(entityType.ClrType).HasQueryFilter(lambda);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|