public void Merge <T>(DbContext context, Type type, IList <T> entities, TableInfo tableInfo, OperationType operationType, Action <decimal> progress) where T : class { var connection = OpenAndGetSqliteConnection(context, tableInfo.BulkConfig); bool doExplicitCommit = false; try { if (context.Database.CurrentTransaction == null) { //context.Database.UseTransaction(connection.BeginTransaction()); doExplicitCommit = true; } var transaction = (SqliteTransaction)(context.Database.CurrentTransaction == null ? connection.BeginTransaction() : context.Database.CurrentTransaction.GetUnderlyingTransaction(tableInfo.BulkConfig)); var command = GetSqliteCommand(context, type, entities, tableInfo, connection, transaction); type = tableInfo.HasAbstractList ? entities[0].GetType() : type; int rowsCopied = 0; foreach (var item in entities) { LoadSqliteValues(tableInfo, item, command); command.ExecuteNonQuery(); ProgressHelper.SetProgress(ref rowsCopied, entities.Count, tableInfo.BulkConfig, progress); } if (operationType != OperationType.Delete && tableInfo.BulkConfig.SetOutputIdentity && tableInfo.IdentityColumnName != null) { command.CommandText = SqlQueryBuilderSqlite.SelectLastInsertRowId(); long lastRowIdScalar = (long)command.ExecuteScalar(); string identityPropertyName = tableInfo.IdentityColumnName; var identityPropertyInteger = false; var identityPropertyUnsigned = false; var identityPropertyByte = false; var identityPropertyShort = false; if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(ulong)) { identityPropertyUnsigned = true; } else if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(uint)) { identityPropertyInteger = true; identityPropertyUnsigned = true; } else if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(int)) { identityPropertyInteger = true; } else if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(ushort)) { identityPropertyShort = true; identityPropertyUnsigned = true; } else if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(short)) { identityPropertyShort = true; } else if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(byte)) { identityPropertyByte = true; identityPropertyUnsigned = true; } else if (tableInfo.FastPropertyDict[identityPropertyName].Property.PropertyType == typeof(sbyte)) { identityPropertyByte = true; } for (int i = entities.Count - 1; i >= 0; i--) { if (identityPropertyByte) { if (identityPropertyUnsigned) { tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (byte)lastRowIdScalar); } else { tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (sbyte)lastRowIdScalar); } } else if (identityPropertyShort) { if (identityPropertyUnsigned) { tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (ushort)lastRowIdScalar); } else { tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (short)lastRowIdScalar); } } else if (identityPropertyInteger) { if (identityPropertyUnsigned) { tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (uint)lastRowIdScalar); } else { tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (int)lastRowIdScalar); } } else { if (identityPropertyUnsigned) { tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], (ulong)lastRowIdScalar); } else { tableInfo.FastPropertyDict[identityPropertyName].Set(entities[i], lastRowIdScalar); } } lastRowIdScalar--; } } if (doExplicitCommit) { transaction.Commit(); } } finally { context.Database.CloseConnection(); } }
protected async Task MergeAsync <T>(DbContext context, Type type, IList <T> entities, TableInfo tableInfo, OperationType operationType, Action <decimal> progress, CancellationToken cancellationToken, bool isAsync) where T : class { SqliteConnection connection = isAsync ? await OpenAndGetSqliteConnectionAsync(context, tableInfo.BulkConfig, cancellationToken).ConfigureAwait(false) : OpenAndGetSqliteConnection(context, tableInfo.BulkConfig); bool doExplicitCommit = false; try { if (context.Database.CurrentTransaction == null) { //context.Database.UseTransaction(connection.BeginTransaction()); doExplicitCommit = true; } var dbTransaction = doExplicitCommit ? connection.BeginTransaction() : context.Database.CurrentTransaction.GetUnderlyingTransaction(tableInfo.BulkConfig); var transaction = (SqliteTransaction)dbTransaction; var command = GetSqliteCommand(context, type, entities, tableInfo, connection, transaction); type = tableInfo.HasAbstractList ? entities[0].GetType() : type; int rowsCopied = 0; foreach (var item in entities) { LoadSqliteValues(tableInfo, item, command, context); if (isAsync) { await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); } else { command.ExecuteNonQuery(); } ProgressHelper.SetProgress(ref rowsCopied, entities.Count, tableInfo.BulkConfig, progress); } if (operationType == OperationType.Insert && tableInfo.BulkConfig.SetOutputIdentity && tableInfo.IdentityColumnName != null) // For Sqlite Identity can be set by Db only with pure Insert method { command.CommandText = SqlQueryBuilderSqlite.SelectLastInsertRowId(); object lastRowIdScalar = isAsync ? await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false) : command.ExecuteScalar(); SetIdentityForOutput(entities, tableInfo, lastRowIdScalar); } if (doExplicitCommit) { transaction.Commit(); } } finally { if (isAsync) { await context.Database.CloseConnectionAsync().ConfigureAwait(false); } else { context.Database.CloseConnection(); } } }