public async Task CheckHasIdentityAsync(DbContext context, CancellationToken cancellationToken) { 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.SelectIdentityColumnName(TableName, Schema); using (var reader = await command.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false)) { if (reader.HasRows) { while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false)) { IdentityColumnName = reader.GetString(0); } } } } } finally { if (currentTransaction == null) { sqlConnection.Close(); } } }
public void CheckHasIdentity(DbContext context) { 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.SelectIdentityColumnName(TableName, Schema); using (var reader = command.ExecuteReader()) { if (reader.HasRows) { while (reader.Read()) { IdentityColumnName = reader.GetString(0); } } } } } finally { if (currentTransaction == null) { sqlConnection.Close(); } } }