public void CheckHasIdentity(DbContext context) { int hasIdentity = 0; var connection = context.Database.GetDbConnection(); try { connection.Open(); using (var command = connection.CreateCommand()) { command.CommandText = SqlQueryBuilder.SelectIsIdentity(FullTableName, PrimaryKey);; using (var reader = command.ExecuteReader()) { if (reader.HasRows) { while (reader.Read()) { hasIdentity = reader[0] == DBNull.Value ? 0 : (int)reader[0]; } } } } } finally { connection.Close(); } HasIdentity = hasIdentity == 1; }
public void CheckHasIdentity(DbContext context) { int hasIdentity = 0; var conn = context.Database.GetDbConnection(); try { conn.OpenAsync(); using (var command = conn.CreateCommand()) { string query = SqlQueryBuilder.SelectIsIdentity(FullTableName, PrimaryKey); command.CommandText = query; DbDataReader reader = command.ExecuteReader(); if (reader.HasRows) { while (reader.Read()) { hasIdentity = (int)reader[0]; } } reader.Dispose(); } } finally { conn.Close(); } HasIdentity = hasIdentity == 1; }
public async Task CheckHasIdentityAsync(DbContext context) { int hasIdentity = 0; var connection = context.Database.GetDbConnection(); try { connection.Open(); using (var command = connection.CreateCommand()) { command.CommandText = SqlQueryBuilder.SelectIsIdentity(FullTableName, PrimaryKey); using (var reader = await command.ExecuteReaderAsync().ConfigureAwait(false)) { if (reader.HasRows) { while (await reader.ReadAsync().ConfigureAwait(false)) { hasIdentity = (int)reader[0]; } } } } } finally { connection.Close(); } HasIdentity = hasIdentity == 1; }
public async Task CheckHasIdentityAsync(DbContext context) { int hasIdentity = 0; if (HasSinglePrimaryKey) { var sqlConnection = context.Database.GetDbConnection(); var currentTransaction = context.Database.CurrentTransaction; try { if (currentTransaction == null) { if (sqlConnection.State != ConnectionState.Open) { await sqlConnection.OpenAsync().ConfigureAwait(false); } } using (var command = sqlConnection.CreateCommand()) { if (currentTransaction != null) { command.Transaction = currentTransaction.GetDbTransaction(); } command.CommandText = SqlQueryBuilder.SelectIsIdentity(FullTableName, PropertyColumnNamesDict[PrimaryKeys[0]]); using (var reader = await command.ExecuteReaderAsync().ConfigureAwait(false)) { if (reader.HasRows) { while (await reader.ReadAsync().ConfigureAwait(false)) { hasIdentity = (int)reader[0]; } } } } } finally { if (currentTransaction == null) { sqlConnection.Close(); } } } HasIdentity = hasIdentity == 1; }
public void CheckHasIdentity(DbContext context) { int hasIdentity = 0; if (HasSinglePrimaryKey) { 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.SelectIsIdentity(FullTableName, PropertyColumnNamesDict[PrimaryKeys[0]]); using (var reader = command.ExecuteReader()) { if (reader.HasRows) { while (reader.Read()) { hasIdentity = reader[0] == DBNull.Value ? 0 : (int)reader[0]; } } } } } finally { if (currentTransaction == null) { sqlConnection.Close(); } } } HasIdentity = hasIdentity == 1; }