public static async Task InsertAsync <T>(DbContext context, IList <T> entities, TableInfo tableInfo, Action <decimal> progress) { var sqlConnection = await OpenAndGetSqlConnectionAsync(context); var transaction = context.Database.CurrentTransaction; try { using (var sqlBulkCopy = GetSqlBulkCopy(sqlConnection, transaction, tableInfo.BulkConfig.SqlBulkCopyOptions)) { bool setColumnMapping = !tableInfo.HasOwnedTypes; tableInfo.SetSqlBulkCopyConfig(sqlBulkCopy, entities, setColumnMapping, progress); try { if (!tableInfo.HasOwnedTypes) { using (var reader = ObjectReaderEx.Create(entities, tableInfo.ShadowProperties, tableInfo.ConvertibleProperties, context, tableInfo.PropertyColumnNamesDict.Keys.ToArray())) { await sqlBulkCopy.WriteToServerAsync(reader).ConfigureAwait(false); } } else { var dataTable = GetDataTable <T>(context, entities); await sqlBulkCopy.WriteToServerAsync(dataTable); } } catch (InvalidOperationException ex) { if (!ex.Message.Contains(ColumnMappingExceptionMessage)) { throw; } await context.Database.ExecuteSqlCommandAsync(SqlQueryBuilder.CreateTableCopy(tableInfo.FullTableName, tableInfo.FullTempTableName, tableInfo)); if (!tableInfo.BulkConfig.UseTempDB) { await context.Database.ExecuteSqlCommandAsync(SqlQueryBuilder.DropTable(tableInfo.FullTempOutputTableName)); } } } } finally { if (transaction == null) { sqlConnection.Close(); } } }
//public static void Insert<T>(DbContext context, IList<T> entities, TableInfo tableInfo, Action<decimal> progress) //{ // var sqlConnection = OpenAndGetSqlConnection(context); // var transaction = context.Database.CurrentTransaction; // try // { // using (var sqlBulkCopy = GetSqlBulkCopy(sqlConnection, transaction, tableInfo.BulkConfig.KeepIdentity)) // { // sqlBulkCopy.BulkCopyTimeout = 960; // tableInfo.SetSqlBulkCopyConfig(sqlBulkCopy, entities, progress); // using (var reader = ObjectReaderEx.Create(entities, tableInfo.ShadowProperties, context, tableInfo.PropertyColumnNamesDict.Keys.ToArray())) // { // sqlBulkCopy.WriteToServer(reader); // } // } // } // finally // { // if (transaction == null) // { // sqlConnection.Close(); // } // } //} #region MainOps public static void Insert <T>(DbContext context, IList <T> entities, TableInfo tableInfo, Action <decimal> progress) { var sqlConnection = OpenAndGetSqlConnection(context); var transaction = context.Database.CurrentTransaction; try { using (var sqlBulkCopy = GetSqlBulkCopy(sqlConnection, transaction, tableInfo.BulkConfig.SqlBulkCopyOptions)) { var setColumnMapping = !tableInfo.HasOwnedTypes; tableInfo.SetSqlBulkCopyConfig(sqlBulkCopy, entities, setColumnMapping, progress); try { if (!tableInfo.HasOwnedTypes) { using (var reader = ObjectReaderEx.Create(entities, tableInfo.ShadowProperties, tableInfo.ConvertibleProperties, context, tableInfo.PropertyColumnNamesDict.Keys.ToArray())) { sqlBulkCopy.WriteToServer(reader); } } else // With OwnedTypes DataTable is used since library FastMember can not (https://github.com/mgravell/fast-member/issues/21) { var dataTable = GetDataTable(context, entities); sqlBulkCopy.WriteToServer(dataTable); } } catch (InvalidOperationException ex) { if (!ex.Message.Contains(ColumnMappingExceptionMessage)) { throw; } context.Database.ExecuteSqlCommand(SqlQueryBuilder.CreateTableCopy(tableInfo.FullTableName, tableInfo.FullTempTableName, tableInfo)); // Exception specify missing db column: Invalid column name '' if (!tableInfo.BulkConfig.UseTempDB) { context.Database.ExecuteSqlCommand(SqlQueryBuilder.DropTable(tableInfo.FullTempOutputTableName)); } } } } finally { if (transaction == null) { sqlConnection.Close(); } } }