public async Task <bool> CheckTableExistAsync(DbContext context, TableInfo tableInfo, CancellationToken cancellationToken) { bool tableExist = false; var sqlConnection = context.Database.GetDbConnection(); var currentTransaction = context.Database.CurrentTransaction; try { if (currentTransaction == null) { if (sqlConnection.State != ConnectionState.Open) { await sqlConnection.OpenAsync(cancellationToken).ConfigureAwait(false); } ; } using (var command = sqlConnection.CreateCommand()) { if (currentTransaction != null) { command.Transaction = currentTransaction.GetDbTransaction(); } command.CommandText = SqlQueryBuilder.CheckTableExist(tableInfo.FullTempTableName, tableInfo.BulkConfig.UseTempDB); using (var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false)) { if (reader.HasRows) { while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) { tableExist = (int)reader[0] == 1; } } } } } finally { if (currentTransaction == null) { sqlConnection.Close(); } } return(tableExist); }
public bool CheckTableExist(DbContext context, TableInfo tableInfo) { bool tableExist = false; var sqlConnection = context.Database.GetDbConnection(); var currentTransaction = context.Database.CurrentTransaction; try { if (currentTransaction == null) { if (sqlConnection.State != ConnectionState.Open) { sqlConnection.Open(); } } using (var command = sqlConnection.CreateCommand()) { if (currentTransaction != null) { command.Transaction = currentTransaction.GetDbTransaction(); } command.CommandText = SqlQueryBuilder.CheckTableExist(tableInfo.FullTempTableName, tableInfo.BulkConfig.UseTempDB); using (var reader = command.ExecuteReader()) { if (reader.HasRows) { while (reader.Read()) { tableExist = (int)reader[0] == 1; } } } } } finally { if (currentTransaction == null) { sqlConnection.Close(); } } return(tableExist); }