У меня есть очень большая таблица в Microsoft Access, которую мне нужно переместить на SQL Server.
Код: Выделить всё
public async Task Sync()
{
using TransactionScope trans = new(TransactionScopeOption.Required, new TransactionOptions()
{
IsolationLevel = IsolationLevel.ReadCommitted,
Timeout = TimeSpan.FromMinutes(60),
}, TransactionScopeAsyncFlowOption.Enabled);
var options = new DbContextOptionsBuilder()
.UseSqlServer(Environment.GetEnvironmentVariable("CS") ?? throw new Exception("CS envar not defined"))
.Options;
await using var context = new FlushpanelContext(options
);
context.Database.SetCommandTimeout(TimeSpan.FromHours(1));
await context.Database.OpenConnectionAsync();
try
{
await using OleDbConnection mydb1 = new($@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={its2000Path};");
await mydb1.OpenAsync();
var partsService = new AccessPartsService(context, its2000Cn, logger);
await partsService.SyncPartsAsync();
trans.Complete();
}
finally
{
await context.Database.CloseConnectionAsync();
}
}
public async Task SyncPartsAsync()
{
// Get all of the parts from access
var accessParts = await accessCn.QueryAsync("SELECT * FROM parts WHERE Imported = 0");
logger.WriteLine($"{accessParts.Count()} parts to be imported in access", Color.DarkBlue);
var partTypes = await context.PartType
.Where(x => x.AccessTableId != null && x.AccessId != null)
.ToDictionaryAsync(x => (x.AccessTableId!.Value, x.AccessId!.Value));
var tablePartTypes = await context.PartType.Where(x => x.AccessTableId == null && x.AccessId == null)
.ToDictionaryAsync(x => x.Title);
await ProcessBatch(accessParts, alreadyExistingSqlParts, manufacturers, partTypes, tablePartTypes);
logger.WriteLine("Saving changes", Color.Purple);
await context.SaveChangesAsync();
await accessCn.ExecuteAsync("UPDATE parts SET Imported = 1 WHERE Imported = 0");
//await transaction.CommitAsync();
}
private async Task ProcessBatch(IEnumerable accessParts, Dictionary alreadyExistingSqlParts, FrozenDictionary manufacturers,
Dictionary partTypes, Dictionary tablePartTypes)
{
// This will run for about 300,000 parts
foreach (var accessPart in accessParts)
{
var converted = accessPart.ToPart(manufacturers);
context.Part.Add(converted);
logger.WriteLine($"Added part {accessPart.Part_ID}", Color.DarkGreen);
}
}
Код: Выделить всё
TransactionScope "A root ambient transaction was completed before the nested transaction"
Подробнее здесь: https://stackoverflow.com/questions/784 ... nested-tra