77 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			77 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using DysonNetwork.Shared.Models;
 | |
| using Microsoft.EntityFrameworkCore;
 | |
| using Microsoft.EntityFrameworkCore.Design;
 | |
| using NodaTime;
 | |
| 
 | |
| namespace DysonNetwork.Insight;
 | |
| 
 | |
| public class AppDatabase(
 | |
|     DbContextOptions<AppDatabase> options,
 | |
|     IConfiguration configuration
 | |
| ) : DbContext(options)
 | |
| {
 | |
|     public DbSet<SnThinkingSequence> ThinkingSequences { get; set; }
 | |
|     public DbSet<SnThinkingThought> ThinkingThoughts { get; set; }
 | |
|     
 | |
|     protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 | |
|     {
 | |
|         optionsBuilder.UseNpgsql(
 | |
|             configuration.GetConnectionString("App"),
 | |
|             opt => opt
 | |
|                 .ConfigureDataSource(optSource => optSource.EnableDynamicJson())
 | |
|                 .UseQuerySplittingBehavior(QuerySplittingBehavior.SplitQuery)
 | |
|                 .UseNodaTime()
 | |
|         ).UseSnakeCaseNamingConvention();
 | |
| 
 | |
|         base.OnConfiguring(optionsBuilder);
 | |
|     }
 | |
|     
 | |
|     public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
 | |
|     {
 | |
|         var now = SystemClock.Instance.GetCurrentInstant();
 | |
| 
 | |
|         foreach (var entry in ChangeTracker.Entries<ModelBase>())
 | |
|         {
 | |
|             switch (entry.State)
 | |
|             {
 | |
|                 case EntityState.Added:
 | |
|                     entry.Entity.CreatedAt = now;
 | |
|                     entry.Entity.UpdatedAt = now;
 | |
|                     break;
 | |
|                 case EntityState.Modified:
 | |
|                     entry.Entity.UpdatedAt = now;
 | |
|                     break;
 | |
|                 case EntityState.Deleted:
 | |
|                     entry.State = EntityState.Modified;
 | |
|                     entry.Entity.DeletedAt = now;
 | |
|                     break;
 | |
|                 case EntityState.Detached:
 | |
|                 case EntityState.Unchanged:
 | |
|                 default:
 | |
|                     break;
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         return await base.SaveChangesAsync(cancellationToken);
 | |
|     }
 | |
| 
 | |
|     protected override void OnModelCreating(ModelBuilder modelBuilder)
 | |
|     {
 | |
|         base.OnModelCreating(modelBuilder);
 | |
|     }
 | |
| }
 | |
| 
 | |
| public class AppDatabaseFactory : IDesignTimeDbContextFactory<AppDatabase>
 | |
| {
 | |
|     public AppDatabase CreateDbContext(string[] args)
 | |
|     {
 | |
|         var configuration = new ConfigurationBuilder()
 | |
|             .SetBasePath(Directory.GetCurrentDirectory())
 | |
|             .AddJsonFile("appsettings.json")
 | |
|             .Build();
 | |
| 
 | |
|         var optionsBuilder = new DbContextOptionsBuilder<AppDatabase>();
 | |
|         return new AppDatabase(optionsBuilder.Options, configuration);
 | |
|     }
 | |
| }
 |