public static void Insert <T>(DbContext context, IList <T> entities, TableInfo tableInfo, Action <decimal> progress) { var sqlConnection = OpenAndGetSqlConnection(context, tableInfo.BulkConfig); var transaction = context.Database.CurrentTransaction; try { using (var sqlBulkCopy = GetSqlBulkCopy(sqlConnection, transaction, tableInfo.BulkConfig)) { bool useFastMember = tableInfo.HasOwnedTypes == false && // With OwnedTypes DataTable is used since library FastMember can not (https://github.com/mgravell/fast-member/issues/21) tableInfo.ShadowProperties.Count == 0 && // With Shadow prop. Discriminator (TPH inheritance) also because FastMember is slow for Update (https://github.com/borisdj/EFCore.BulkExtensions/pull/17) tableInfo.ColumnNameContainsSquareBracket == false; // FastMember does not support escaped columnNames ] -> ]] bool setColumnMapping = useFastMember; tableInfo.SetSqlBulkCopyConfig(sqlBulkCopy, entities, setColumnMapping, progress); try { if (useFastMember) { using (var reader = ObjectReaderEx.Create(entities, tableInfo.ShadowProperties, tableInfo.ConvertibleProperties, context, tableInfo.PropertyColumnNamesDict.Keys.ToArray())) { sqlBulkCopy.WriteToServer(reader); } } else { var dataTable = GetDataTable <T>(context, entities, sqlBulkCopy, tableInfo); sqlBulkCopy.WriteToServer(dataTable); } } catch (InvalidOperationException ex) { if (!tableInfo.BulkConfig.UseTempDB) { context.Database.ExecuteSqlCommand(SqlQueryBuilder.DropTable(tableInfo.FullTempOutputTableName)); } if (ex.Message.Contains(ColumnMappingExceptionMessage)) { if (!tableInfo.CheckTableExist(context, tableInfo)) { context.Database.ExecuteSqlCommand(SqlQueryBuilder.CreateTableCopy(tableInfo.FullTableName, tableInfo.FullTempTableName, tableInfo)); // Will throw Exception specify missing db column: Invalid column name '' context.Database.ExecuteSqlCommand(SqlQueryBuilder.DropTable(tableInfo.FullTempTableName)); } } throw ex; } } } finally { if (transaction == null) { sqlConnection.Close(); } } }
public static void Insert <T>(DbContext context, IList <T> entities, TableInfo tableInfo, Action <decimal> progress) { string providerName = context.Database.ProviderName; // "Microsoft.EntityFrameworkCore.*****" // -- SQL Server -- if (providerName.EndsWith(DbServer.SqlServer.ToString())) { var connection = OpenAndGetSqlConnection(context, tableInfo.BulkConfig); var transaction = context.Database.CurrentTransaction; try { using (var sqlBulkCopy = GetSqlBulkCopy((SqlConnection)connection, transaction, tableInfo.BulkConfig)) { bool useFastMember = tableInfo.HasOwnedTypes == false && // With OwnedTypes DataTable is used since library FastMember can not (https://github.com/mgravell/fast-member/issues/21) tableInfo.ColumnNameContainsSquareBracket == false && // FastMember does not support escaped columnNames ] -> ]] tableInfo.ShadowProperties.Count == 0 && // With Shadow prop. Discriminator (TPH inheritance) also not used because FastMember is slow for Update (https://github.com/borisdj/EFCore.BulkExtensions/pull/17) !tableInfo.ConvertibleProperties.Any() && // With ConvertibleProperties FastMember is slow as well !tableInfo.HasAbstractList && // AbstractList not working with FastMember !tableInfo.BulkConfig.UseOnlyDataTable; bool setColumnMapping = useFastMember; tableInfo.SetSqlBulkCopyConfig(sqlBulkCopy, entities, setColumnMapping, progress); try { if (useFastMember) { using (var reader = ObjectReaderEx.Create(entities, tableInfo.ShadowProperties, tableInfo.ConvertibleProperties, context, tableInfo.PropertyColumnNamesDict.Keys.ToArray())) { sqlBulkCopy.WriteToServer(reader); } } else { var dataTable = GetDataTable <T>(context, entities, sqlBulkCopy, tableInfo); sqlBulkCopy.WriteToServer(dataTable); } } catch (InvalidOperationException ex) { if (ex.Message.Contains(ColumnMappingExceptionMessage)) { if (!tableInfo.CheckTableExist(context, tableInfo)) { context.Database.ExecuteSqlRaw(SqlQueryBuilder.CreateTableCopy(tableInfo.FullTableName, tableInfo.FullTempTableName, tableInfo)); // Will throw Exception specify missing db column: Invalid column name '' context.Database.ExecuteSqlRaw(SqlQueryBuilder.DropTable(tableInfo.FullTempTableName)); } } throw ex; } } } finally { if (transaction == null) { connection.Close(); } } } // -- SQLite -- else if (providerName.EndsWith(DbServer.Sqlite.ToString())) { var connection = OpenAndGetSqliteConnection(context, tableInfo.BulkConfig); var transaction = tableInfo.BulkConfig.SqliteTransaction ?? connection.BeginTransaction(); try { var command = GetSqliteCommand(context, entities, tableInfo, connection, transaction); var typeAccessor = TypeAccessor.Create(typeof(T), true); int rowsCopied = 0; foreach (var item in entities) { LoadSqliteValues(tableInfo, typeAccessor, item, command); command.ExecuteNonQuery(); SetProgress(ref rowsCopied, entities.Count, tableInfo.BulkConfig, progress); } } finally { if (tableInfo.BulkConfig.SqliteTransaction == null) { transaction.Commit(); transaction.Dispose(); } if (tableInfo.BulkConfig.SqliteConnection == null) { connection.Close(); } } } else { throw new SqlProviderNotSupportedException(providerName); } }