50 lines
1.8 KiB
C#
50 lines
1.8 KiB
C#
using System.Reflection;
|
|
using CMSMicroservice.Application.Common.Interfaces;
|
|
using CMSMicroservice.Domain.Entities;
|
|
using CMSMicroservice.Infrastructure.Persistence.Interceptors;
|
|
using MediatR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace CMSMicroservice.Infrastructure.Persistence;
|
|
|
|
public class ApplicationDbContext : DbContext, IApplicationDbContext
|
|
{
|
|
private readonly IMediator _mediator;
|
|
private readonly AuditableEntitySaveChangesInterceptor _auditableEntitySaveChangesInterceptor;
|
|
|
|
public ApplicationDbContext(
|
|
DbContextOptions<ApplicationDbContext> options,
|
|
IMediator mediator,
|
|
AuditableEntitySaveChangesInterceptor auditableEntitySaveChangesInterceptor)
|
|
: base(options)
|
|
{
|
|
_mediator = mediator;
|
|
_auditableEntitySaveChangesInterceptor = auditableEntitySaveChangesInterceptor;
|
|
}
|
|
protected override void OnModelCreating(ModelBuilder builder)
|
|
{
|
|
builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());
|
|
builder.HasDefaultSchema("CMS");
|
|
base.OnModelCreating(builder);
|
|
}
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
optionsBuilder.AddInterceptors(_auditableEntitySaveChangesInterceptor);
|
|
}
|
|
|
|
public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
|
|
{
|
|
await _mediator.DispatchDomainEvents(this);
|
|
|
|
return await base.SaveChangesAsync(cancellationToken);
|
|
}
|
|
public DbSet<UserAddress> UserAddresss => Set<UserAddress>();
|
|
public DbSet<Package> Packages => Set<Package>();
|
|
public DbSet<UserOrder> UserOrders => Set<UserOrder>();
|
|
public DbSet<Role> Roles => Set<Role>();
|
|
public DbSet<UserRole> UserRoles => Set<UserRole>();
|
|
public DbSet<OtpToken> OtpTokens => Set<OtpToken>();
|
|
public DbSet<User> Users => Set<User>();
|
|
|
|
} |