public static void Execute <T>(DbContext context, IList <T> entities, OperationType operationType, BulkConfig bulkConfig, Action <decimal> progress) where T : class { if (operationType != OperationType.Truncate && entities.Count == 0) { return; } TableInfo tableInfo = TableInfo.CreateInstance(context, entities, operationType, bulkConfig); if (operationType == OperationType.Insert && !tableInfo.BulkConfig.SetOutputIdentity) { SqlBulkOperation.Insert(context, entities, tableInfo, progress); } else if (operationType == OperationType.Read) { SqlBulkOperation.Read(context, entities, tableInfo, progress); } else if (operationType == OperationType.Truncate) { SqlBulkOperation.Truncate(context, tableInfo); } else { SqlBulkOperation.Merge(context, entities, tableInfo, operationType, progress); } }
public static Task ExecuteAsync <T>(DbContext context, IList <T> entities, OperationType operationType, BulkConfig bulkConfig, Action <decimal> progress, CancellationToken cancellationToken) where T : class { if (operationType != OperationType.Truncate && entities.Count == 0) { return(Task.CompletedTask); } TableInfo tableInfo = TableInfo.CreateInstance(context, entities, operationType, bulkConfig); if (operationType == OperationType.Insert && !tableInfo.BulkConfig.SetOutputIdentity) { return(SqlBulkOperation.InsertAsync(context, entities, tableInfo, progress, cancellationToken)); } else if (operationType == OperationType.Read) { return(SqlBulkOperation.ReadAsync(context, entities, tableInfo, progress, cancellationToken)); } else if (operationType == OperationType.Truncate) { return(SqlBulkOperation.TruncateAsync(context, tableInfo)); } else { return(SqlBulkOperation.MergeAsync(context, entities, tableInfo, operationType, progress, cancellationToken)); } }
public static DbTransaction GetUnderlyingTransaction(this IDbContextTransaction ctxTransaction, BulkConfig config) { var dbTransaction = ctxTransaction.GetDbTransaction(); if (config?.UnderlyingTransaction != null) { dbTransaction = config.UnderlyingTransaction(dbTransaction); } return(dbTransaction); }
// Async methods public static Task BulkInsertAsync <T>(this DbContext context, IList <T> entities, BulkConfig bulkConfig = null, Action <decimal> progress = null, CancellationToken cancellationToken = default) where T : class { return(DbContextBulkTransaction.ExecuteAsync(context, entities, OperationType.Insert, bulkConfig, progress, cancellationToken)); }
public static void BulkRead <T>(this DbContext context, IList <T> entities, BulkConfig bulkConfig = null, Action <decimal> progress = null) where T : class { DbContextBulkTransaction.Execute(context, entities, OperationType.Read, bulkConfig, progress); }
public static void BulkInsertOrUpdateOrDelete <T>(this DbContext context, IList <T> entities, BulkConfig bulkConfig = null, Action <decimal> progress = null) where T : class { DbContextBulkTransaction.Execute(context, entities, OperationType.InsertOrUpdateDelete, bulkConfig, progress); }
public static TableInfo CreateInstance <T>(DbContext context, IList <T> entities, OperationType operationType, BulkConfig bulkConfig) { var tableInfo = new TableInfo { NumberOfEntities = entities.Count, BulkConfig = bulkConfig ?? new BulkConfig() { } }; tableInfo.BulkConfig.OperationType = operationType; bool isExplicitTransaction = context.Database.GetDbConnection().State == ConnectionState.Open; if (tableInfo.BulkConfig.UseTempDB == true && !isExplicitTransaction && (operationType != OperationType.Insert || tableInfo.BulkConfig.SetOutputIdentity)) { throw new InvalidOperationException("UseTempDB when set then BulkOperation has to be inside Transaction. More info in README of the library in GitHub."); // Otherwise throws exception: 'Cannot access destination table' (gets Dropped too early because transaction ends before operation is finished) } var isDeleteOperation = operationType == OperationType.Delete; tableInfo.LoadData <T>(context, entities, isDeleteOperation); return(tableInfo); }