- Changed DbTriggerParams to use ICollection<string> for flexibility. - Updated Microsoft.EntityFrameworkCore.SqlServer to version 9.0.6. - Made _logger in EGDbContext nullable and optional in constructor. - Updated logging statements to prevent null reference exceptions. - Added Microsoft.EntityFrameworkCore.SqlServer package for net8.0 and net9.0. - Introduced appsettings.migration.json for connection strings and trigger parameters. - Added EGDbContextFactory for design-time DbContext creation.
44 lines
1.6 KiB
C#
44 lines
1.6 KiB
C#
using EnvelopeGenerator.Application.Configurations;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Design;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Options;
|
|
|
|
namespace EnvelopeGenerator.Infrastructure
|
|
{
|
|
public class EGDbContextFactory : IDesignTimeDbContextFactory<EGDbContext>
|
|
{
|
|
public EGDbContext CreateDbContext(string[] args)
|
|
{
|
|
var config = new ConfigurationBuilder()
|
|
.SetBasePath(Directory.GetCurrentDirectory()) // Önemli!
|
|
.AddJsonFile("appsettings.migration.json")
|
|
.Build();
|
|
|
|
// create DbContextOptions
|
|
var optionsBuilder = new DbContextOptionsBuilder<EGDbContext>();
|
|
optionsBuilder.UseSqlServer(config.GetConnectionString("Default"));
|
|
|
|
// create DbTriggerParams
|
|
var triggerLists = config.GetSection("DbTriggerParams").Get<Dictionary<string, List<string>>>();
|
|
var dbTriggerParams = new DbTriggerParams();
|
|
if(triggerLists is not null)
|
|
foreach (var triggerList in triggerLists)
|
|
{
|
|
if(triggerList.Value.Count == 0)
|
|
continue; // Skip empty trigger lists
|
|
|
|
var tableName = triggerList.Key;
|
|
|
|
dbTriggerParams[tableName] = new List<string>();
|
|
|
|
foreach (var trigger in triggerList.Value)
|
|
{
|
|
dbTriggerParams[tableName].Add(trigger);
|
|
}
|
|
}
|
|
|
|
return new EGDbContext(optionsBuilder.Options, Options.Create(dbTriggerParams));
|
|
}
|
|
}
|
|
} |