Files
CMS/src/CMSMicroservice.Infrastructure/Persistence/ApplicationDbContext.cs
2025-11-25 02:03:51 +03:30

64 lines
2.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<Role> Roles => Set<Role>();
public DbSet<Category> Categorys => Set<Category>();
public DbSet<UserRole> UserRoles => Set<UserRole>();
public DbSet<UserCarts> UserCartss => Set<UserCarts>();
public DbSet<ProductGallerys> ProductGalleryss => Set<ProductGallerys>();
public DbSet<FactorDetails> FactorDetailss => Set<FactorDetails>();
public DbSet<Products> Productss => Set<Products>();
public DbSet<ProductImages> ProductImagess => Set<ProductImages>();
public DbSet<User> Users => Set<User>();
public DbSet<OtpToken> OtpTokens => Set<OtpToken>();
public DbSet<Contract> Contracts => Set<Contract>();
public DbSet<UserContract> UserContracts => Set<UserContract>();
public DbSet<Tag> Tags => Set<Tag>();
public DbSet<PruductCategory> PruductCategorys => Set<PruductCategory>();
public DbSet<PruductTag> PruductTags => Set<PruductTag>();
public DbSet<Transactions> Transactionss => Set<Transactions>();
public DbSet<UserOrder> UserOrders => Set<UserOrder>();
public DbSet<UserWallet> UserWallets => Set<UserWallet>();
public DbSet<UserWalletChangeLog> UserWalletChangeLogs => Set<UserWalletChangeLog>();
}