Код: Выделить всё
SourceDbContext
Однако, когда я пытаюсь напечатать строку подключения для SourceDbContext он всегда использует строку подключения для TargetDbContext (
Код: Выделить всё
DefaultConnection
Вот мой код — Program.cs:
Код: Выделить всё
builder.Services.AddDbContext(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection"))
.AddInterceptors(new PerformanceInterceptor()));
builder.Services.AddDbContext(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("PreviousConnection")));
builder.Services.AddDbContext(options =>
options.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")));
Код: Выделить всё
DataMigrationService:
public class DataMigrationService
{
private readonly SourceDbContext _sourceContext;
private readonly TargetDbContext _targetContext;
public DataMigrationService(SourceDbContext sourceContext, TargetDbContext targetContext)
{
_sourceContext = sourceContext;
_targetContext = targetContext;
}
public async Task MigrateTableDataAsync()
{
Console.WriteLine($"Source DB Connection String: {_sourceContext.Database.GetConnectionString()}");
Console.WriteLine($"Target DB Connection String: {_targetContext.Database.GetConnectionString()}");
// Fetch data from source DB
var sourceWhiteLists = await _sourceContext.EmailDomainWhiteLists.ToListAsync();
// Fetch data from target DB
var targetWhiteLists = await _targetContext.EmailDomainWhiteLists.ToListAsync();
foreach (var wl in sourceWhiteLists)
{
var targetWL = await _targetContext.EmailDomainWhiteLists.FirstOrDefaultAsync(x => x.Value == wl.Value);
if (targetWL == null)
{
_targetContext.EmailDomainWhiteLists.Add(wl);
}
}
await _targetContext.SaveChangesAsync();
Console.WriteLine("Migration completed.");
}
}
Код: Выделить всё
SourceDbContext:
public class SourceDbContext : ApplicationDbContext
{
public SourceDbContext(DbContextOptions options)
: base(options)
{
}
}
Как я могу гарантировать, что SourceDbContext использует своя собственная строка подключения (
Код: Выделить всё
PreviousConnection
Заранее спасибо!
Дополнительная информация:
- Я попробовал проверить строку подключения с помощью Console.WriteLine, и стало ясно, что SourceDbContext все еще использует DefaultConnection
- Мне нужны SourceDbContext и TargetDbContext, чтобы использовать соответствующие строки подключения для сред разработки и контроля качества
Подробнее здесь: https://stackoverflow.com/questions/790 ... ng-data-be