コード例 #1
0
        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);
            }
        }
コード例 #2
0
        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));
            }
        }
コード例 #3
0
        public static DbTransaction GetUnderlyingTransaction(this IDbContextTransaction ctxTransaction, BulkConfig config)
        {
            var dbTransaction = ctxTransaction.GetDbTransaction();

            if (config?.UnderlyingTransaction != null)
            {
                dbTransaction = config.UnderlyingTransaction(dbTransaction);
            }
            return(dbTransaction);
        }
コード例 #4
0
        // 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));
        }
コード例 #5
0
 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);
 }
コード例 #6
0
 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);
 }
コード例 #7
0
        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);
        }