123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- #nullable disable
- using AipGateway.API.Domain.Entities;
- using AipGateway.API.Domain.IServices.IUtilities;
- using AipGateway.API.Infrastructure.Configurations;
- using Microsoft.EntityFrameworkCore;
- using Microsoft.Extensions.Logging;
- using System.Data;
- namespace AipGateway.API.Infrastructure.Persistence
- {
- public class ApplicationDbContext : DbContext
- {
- private readonly ILogger<ApplicationDbContext> _log;
- private readonly IDomainEventDispatcher _dispatcher;
- private readonly ConnectionInfo _connectionInfo;
- public ApplicationDbContext(ILogger<ApplicationDbContext> logger, DbContextOptions<ApplicationDbContext> options, ConnectionInfo connectionInfo) : base(options)
- {
- _log = logger;
- _connectionInfo = connectionInfo;
- }
- protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
- {
- optionsBuilder.UseSqlServer(_connectionInfo.ConnectionString, b => b.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName));
- }
- protected override void OnModelCreating(ModelBuilder builder)
- {
- #if false
- //ApplicationDbContextSeed.SeedSampleDataAsync(modelBuilder); ==> 테스트 데이터 넣을 경우
- foreach (var entityType in builder.Model.GetEntityTypes())
- {
- //get first property of entity with DatabaseGeneratedAttribute = DatabaseGeneratedOption.Identity
- var idProp = entityType.GetProperties()
- .FirstOrDefault(x => x.PropertyInfo != null
- && x.PropertyInfo.GetCustomAttribute<DatabaseGeneratedAttribute>()
- ?.DatabaseGeneratedOption == DatabaseGeneratedOption.Identity);
- //if it exists, set it to identity
- if (idProp != null)
- {
- builder.Entity(entityType.ClrType)
- .Property(idProp.Name)
- .ValueGeneratedOnAddOrUpdate()
- .UseIdentityColumn();
- }
- }
- #endif
- }
- public override int SaveChanges()
- {
- //var userID = _currentUserService.ID;
- //if (userID == 0)
- //{
- // return base.SaveChanges();
- //}
- foreach (var entry in ChangeTracker.Entries<BaseEntity>())
- {
- switch (entry.State)
- {
- case EntityState.Added:
- //entry.Entity.CreatedBy = userID;
- entry.Entity.CreatedAt = DateTime.UtcNow;
- entry.Entity.IsActive = true;
- entry.Entity.IsDeleted = false;
- break;
- case EntityState.Modified:
- //entry.Entity.ModifiedBy = userID;
- entry.Entity.ModifiedAt = DateTime.UtcNow;
- break;
- }
- }
-
- //foreach (var entry in ChangeTracker.Entries<AuditableEntity>())
- //{
- // switch (entry.State)
- // {
- // case EntityState.Added:
- // entry.Entity.CreatedBy = userID;
- // entry.Entity.CreatedAt = DateTime.UtcNow;
- // break;
- // case EntityState.Modified:
- // entry.Entity.ModifiedBy = userID;
- // entry.Entity.ModifiedAt = DateTime.UtcNow;
- // break;
- // }
- //}
- //foreach (var entry in ChangeTracker.Entries<UserRole>())
- //{
- // switch (entry.State)
- // {
- // case EntityState.Added:
- // entry.Entity.CreatedBy = userID;
- // entry.Entity.CreatedAt = DateTime.UtcNow;
- // break;
- // case EntityState.Modified:
- // entry.Entity.ModifiedBy = userID;
- // entry.Entity.ModifiedAt = DateTime.UtcNow;
- // break;
- // }
- //}
- PreSaveChanges().GetAwaiter().GetResult();
- var response = base.SaveChanges();
- PostSaveChanges().GetAwaiter().GetResult();
- return response;
- }
- private async Task PreSaveChanges()
- {
- await Task.CompletedTask;
- }
- private async Task PostSaveChanges()
- {
- await DispatchDomainEvents();
- }
- private async Task DispatchDomainEvents()
- {
- var domainEventEntities = ChangeTracker.Entries<IHasDomainEventEntity>()
- .Select(po => po.Entity)
- .Where(po => po.DomainEvents.Any())
- .ToArray();
- foreach (var entity in domainEventEntities)
- {
- while (entity.DomainEvents.TryTake(out IDomainEvent dev))
- await _dispatcher.Dispatch(dev);
- }
- }
- public IEnumerable<T> SqlQuery<T>(FormattableString sql)
- {
- try
- {
- return Database.SqlQuery<T>(sql);
- }
- catch (Exception ex)
- {
- _log.LogError("SqlQuery: {0}", sql.ToString());
- _log.LogError("SqlQuery: {0}", ex.Message);
- }
- return null;
- }
- public int ExecuteSql(FormattableString sql)
- {
- try
- {
- return Database.ExecuteSql(sql);
- }
- catch (Exception ex)
- {
- _log.LogError("ExecuteSql: {0}", sql.ToString());
- _log.LogError("ExecuteSql: {0}", ex.Message);
- }
- return -1;
- }
- public virtual DbSet<TbLinkedSystem> LinkedSystems { get; set; }
- public virtual DbSet<TbLinkedServer> LinkedServers { get; set; }
- public virtual DbSet<TbLinkedDecryptKey> LinkedDecryptKeys { get; set; }
- public virtual DbSet<TbLinkedApiKey> LinkedApiKeys { get; set; }
- public virtual DbSet<TbAipLabel> AipLabels { get; set; }
- public virtual DbSet<TbAipPolicy> AipPolicies { get; set; }
- public virtual DbSet<TbAipProtection> AipProtections { get; set; }
- public virtual DbSet<TbAipServer> AipServers { get; set; }
- public virtual DbSet<TbAipConfig> AipConfigs { get; set; }
- public virtual DbSet<TbAipFileJobLog> AipFileJobLogs { get; set; }
- public virtual DbSet<TbAipApiCallLog> AipApiCallLogs { get; set; }
- }
- }
|