Skip to content

DbSession Class ​

Definition ​

Class representing a Connection to a database

  • Namespace: JASE.Database
  • Assembly: JASE.Database.dll

Properties ​

NameDescription
CommandHistoryCommand Execution history for this Connnection
CommandHistoryMaxSizeMaximum number of commands tracked in CommandHistory
CommandHistoryTrackingIs command history tracking enabled on this connection?
ConnectionStringConnection string used to establish the connection. Visibility is managed by Configuration.ExposeConnectionStrings
ConnectionStringHashSHA256 hash of the connection string
DatabaseTypeString representing the type of database
DiagnosticsDiagnostic level for the current Connection
GracefulReaderShutdownDetermines whether the connection should force-close open readers on close.
InTransactionReturns when the connection is in an existing TRANSACTION, otherwise
IsOpenIndicates if the connection is currently open.
LastParametersRead only array of the parameters used in the Last SQL statement
LastSQLThe most recent SQL statement executed on the connection
MaxParametersMaximum number of parameters in a single statement supported by the current database
SqlGeneratorA SqlGenerator for the currently connected database
StateCurrent state of the underlying connection to the database
TransactionCountGets the count of over-the-wire transactions executed for the Session's connection object
UuidUnique identifier for the database connection

Methods ​

NameDescription
Task<bool> AcquireDatabaseLockAsync(string resource, DatabaseLockMode mode, DatabaseLockOwner owner, TimeSpan? timeout = null, CancellationToken cancellationToken = null)Attempts to acquire a database-backed application lock.
Task<Nullable<decimal>> AverageAsync(Expression<Func<T, object>> property, bool distinct = null, IPredicateNode predicate = null, CancellationToken cancellationToken = null)Computes the average of the specified property for rows matching the optional predicate.
void BeginTransaction(TransactionIsolation level = null)Begin a new TRANSACTION
Task<int> BulkInsertAsync(IEnumerable<T> list, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null)Inserts dynamic rows using provider-specific bulk techniques (binary COPY, SqlBulkCopy, etc.). Falls back to InsertListAsync when no bulk provider is registered.
Task<int> BulkInsertAsync(TableInfo table, IEnumerable<DynamicRow> rows, CancellationToken cancellationToken = null)Inserts dynamic rows using provider-specific bulk techniques (binary COPY, SqlBulkCopy, etc.). Falls back to InsertListAsync when no bulk provider is registered.
Task<int> BulkInsertAsync(Type entityType, IEnumerable list, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null)Inserts dynamic rows using provider-specific bulk techniques (binary COPY, SqlBulkCopy, etc.). Falls back to InsertListAsync when no bulk provider is registered.
Task<int> BulkUpdateAsync(IEnumerable<T> list, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null)Updates a list of existing rows using provider-specific bulk techniques.
Task<int> BulkUpdateAsync(Type entityType, IEnumerable list, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null)Updates a list of existing rows using provider-specific bulk techniques and a runtime entity type.
int CalculateCommitInterval(int requestedInterval, int parameterCount)Calculate the max rows we can safely commit within a single statement based on a particular database server's ability to handle parameters
bool CanResolveConnectionType(string dbType)Verify that Registered database can generate a connection
bool ChangeDatabase(string database)Change to the requested database
Task<bool> ChangeDatabaseAsync(string database)Change to the requested database
bool CheckDatabaseExist(string database)Check if the specified database exist
bool CheckSchemaExist(string schema)Checks if the specified Schema exist in the database
Task<bool> CheckSchemaExistAsync(string schema)Checks if the specified Schema exist in the database
bool CheckTableExist(ISelectable tableInfo)Checks if the specified Table exist in the database
bool CheckTableExist(string table, string schema = null)Checks if the specified Table exist in the database
Task<bool> CheckTableExistAsync(ISelectable tableInfo)Checks if the specified Table exist in the database
Task<bool> CheckTableExistAsync(string table, string schema = null)Checks if the specified Table exist in the database
bool Close()Close connection to the database server
Task CloseAsync()Close the connection asynchronously.
void Commit()Commit the current TRANSACTION
Task CommitAsync()Commit the current TRANSACTION
Task<long> CountAsync(IPredicateNode query = null)Returns the count of records matching the optional predicate.
Task<long> CountAsync(Type entityType, IPredicateNode query = null)Returns the count of records matching the optional predicate for a runtime entity type.
bool CreateDatabase(string database)Create a new database
bool CreateSchema(string schema, string authorization = null, string comment = null)Create a schema in the database
Task<bool> CreateSchemaAsync(string schema, string authorization = null, string comment = null)Create a schema in the database
Task CreateViewAsync(string name, QueryBuilder query, string schema = null)Creates or replaces a database view from a parameter-free QueryBuilder SELECT.
Task CreateViewAsync(string name, SelectStatement select, string schema = null)Creates or replaces a database view from a parameter-free SelectStatement. Prefer overloads that accept a QueryBuilder when composing from the fluent API.
Task<bool> DeleteAsync(T item, int commandTimeout = null, CancellationToken cancellationToken = null)Deletes a record using the entity primary key value.
Task<bool> DeleteAsync(Type entityType, object item, int commandTimeout = null, CancellationToken cancellationToken = null)Deletes a record using a runtime entity type and instance.
Task<bool> DeleteByPkAsync(TableInfo table, object id, int commandTimeout = null, CancellationToken cancellationToken = null)Deletes a row by primary key value.
Task<bool> DeleteByPkAsync(Type entityType, object id, int commandTimeout = null, CancellationToken cancellationToken = null)Deletes a row by primary key value.
Task<bool> DeleteByPkAsync(object id, int commandTimeout = null, CancellationToken cancellationToken = null)Deletes a row by primary key value.
Task<int> DeleteListAsync(IEnumerable<T> list, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null)Deletes a list of records using each item's primary key value.
Task<int> DeleteListAsync(Type entityType, IEnumerable list, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null)Deletes a list of records using a runtime entity type and each item's primary key value.
Task<int> DeleteListByPkAsync(IEnumerable<object> ids, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null)Deletes a list of records by primary key values.
Task<int> DeleteListByPkAsync(Type entityType, IEnumerable<object> ids, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null)Deletes a list of records by primary key values using a runtime entity type.
void Dispose()awaits a call to DisposeAsync occurs here
ValueTask DisposeAsync()Calls DisposeAsyncCore() and GC.SuppressFinalize
Task<bool> DropConstraintAsync(Constraint constraint)Drop the requested Constraint from the table
Task<bool> DropConstraintAsync(ISelectable selectable, string constraint)Drop the requested Constraint from the table
Task<bool> DropConstraintAsync(string table, string constraint)Drop the requested Constraint from the table
bool DropDatabase(string database)Execute a DROP command for the specified database
bool DropSchema(string schema)Execute a DROP command for the specified schema
Task<bool> DropSchemaAsync(string schema)Execute a DROP command for the specified schema
bool DropTable(TableInfo tableInfo)Execute a DROP command for the named table
bool DropTable(string table)Execute a DROP command for the named table
Task<bool> DropTableAsync(TableInfo tableInfo)Execute a DROP command for the named table
Task<bool> DropTableAsync(string tableInfo)Execute a DROP command for the named table
bool DropTables(IEnumerable<string> tables)Execute a DROP command for multiple tables
Task<bool> DropTablesAsync(IEnumerable<string> tables)Execute a DROP command for multiple tables
Task DropViewAsync(string name, string schema = null)Drops a database view when it exists.
NonQueryResult ExecuteNonQuery(string command, IReadOnlyList<IParameter> parameters = null, bool allowBatch = null, CommandType commandType = null, int commandTimeout = null)Execute a Non-Query operation on the database connection returning TRUE for success
Task<NonQueryResult> ExecuteNonQueryAsync(string command, IReadOnlyList<IParameter> parameters = null, bool allowBatch = null, CommandType commandType = null, int commandTimeout = null, CancellationToken cancellationToken = null)Asynchronously execute a Non-Query operation on the database connection returning TRUE for success
Reader ExecuteReader(string command, IReadOnlyList<IParameter> parameters = null, bool doFirstRead = null, bool allowBatch = null, CommandType commandType = null, bool enableReaderTracking = null, int commandTimeout = null)Executes a database query and returns a Reader to read the results.
Task<Reader> ExecuteReaderAsync(ISqlStatement statement, bool doFirstRead = null, bool allowBatch = null, CommandType commandType = null, bool enableReaderTracking = null, int commandTimeout = null, CancellationToken cancellationToken = null)Executes a database query asynchronously and returns a Reader to read the results.
Task<Reader> ExecuteReaderAsync(string command, IReadOnlyList<IParameter> parameters = null, bool doFirstRead = null, bool allowBatch = null, CommandType commandType = null, bool enableReaderTracking = null, int commandTimeout = null, CancellationToken cancellationToken = null)Executes a database query asynchronously and returns a Reader to read the results.
T ExecuteScalar(string command, IReadOnlyList<IParameter> parameters = null, bool allowBatch = null, CommandType commandType = null, int commandTimeout = null)Execute command against the existing connection and return Scalar object
T ExecuteScalar(string command, out TimeSpan timings, IReadOnlyList<IParameter> parameters = null, bool allowBatch = null, CommandType commandType = null, int commandTimeout = null)Execute command against the existing connection and return Scalar object
object ExecuteScalar(string command, IReadOnlyList<IParameter> parameters = null, bool allowBatch = null, CommandType commandType = null, int commandTimeout = null)Execute command against the existing connection and return Scalar object
object ExecuteScalar(string command, out TimeSpan timings, IReadOnlyList<IParameter> parameters = null, bool allowBatch = null, CommandType commandType = null, int commandTimeout = null)Execute command against the existing connection and return Scalar object
Task<T> ExecuteScalarAsync(ISqlStatement statement, bool allowBatch = null, CommandType commandType = null, int commandTimeout = null, CancellationToken cancellationToken = null)Asynchronously execute command against the existing connection and return Scalar object
Task<T> ExecuteScalarAsync(string command, IReadOnlyList<IParameter> parameters = null, bool allowBatch = null, CommandType commandType = null, int commandTimeout = null, CancellationToken cancellationToken = null)Asynchronously execute command against the existing connection and return Scalar object
Task<object> ExecuteScalarAsync(ISqlStatement statement, bool allowBatch = null, CommandType commandType = null, int commandTimeout = null, CancellationToken cancellationToken = null)Asynchronously execute command against the existing connection and return Scalar object
Task<object> ExecuteScalarAsync(string command, IReadOnlyList<IParameter> parameters = null, bool allowBatch = null, CommandType commandType = null, int commandTimeout = null, CancellationToken cancellationToken = null)Asynchronously execute command against the existing connection and return Scalar object
Task<QueryPlanResult> ExplainAsync(ISqlStatement statement, QueryPlanOptions options = null, CancellationToken cancellationToken = null)Requests an explain / showplan for the specified statement and returns a normalized result.
Task<QueryPlanResult> ExplainAsync(string command, IReadOnlyList<IParameter> parameters = null, QueryPlanOptions options = null, CancellationToken cancellationToken = null)Requests an explain / showplan for the specified SQL text and returns a normalized result.
Task<T> FirstOrDefault(QueryBuilder<T> query, CancellationToken cancellationToken = null)Returns the first record matching the provided query, or null if no rows match.
Guid GenerateUuid()Generate a UUID byte ordered for the current connection type
IDatabaseSession Get(DatabaseConfig config, ISqlGeneratorOptions sqlGeneratorOptions = null, string database = null, bool openConnection = null)Obtain a database session.
IDatabaseSession Get(DatabaseType dbType, string connectionString, ISqlGeneratorOptions sqlGeneratorOptions = null, string database = null, bool openConnection = null, LogConfig logConfig = null)Obtain session connection to a database
IDatabaseSession Get(string dbType, string connectionString, ISqlGeneratorOptions sqlGeneratorOptions = null, string database = null, bool openConnection = null, LogConfig logConfig = null)Obtain session connection to a database
Task<IDatabaseSession> GetAsync(DatabaseConfig config, ISqlGeneratorOptions sqlGeneratorOptions = null, string database = null, bool openConnection = null, CancellationToken ct = null)Obtain a database session asynchronously.
Task<IDatabaseSession> GetAsync(DatabaseType dbType, string connectionString, ISqlGeneratorOptions sqlGeneratorOptions = null, string database = null, bool openConnection = null, LogConfig logConfig = null)Obtain session connection to a database
Task<IDatabaseSession> GetAsync(string dbType, string connectionString, ISqlGeneratorOptions sqlGeneratorOptions = null, string database = null, bool openConnection = null, LogConfig logConfig = null)Obtain session connection to a database
Task<DynamicRow> GetByPkAsync(TableInfo table, object id, CancellationToken cancellationToken = null)Fetches a row by primary key value, or null if none match.
Task<T> GetByPkAsync(object id, CancellationToken cancellationToken = null)Fetches a row by primary key value, or null if none match.
Task<object> GetByPkAsync(Type entityType, object id, CancellationToken cancellationToken = null)Fetches a row by primary key value, or null if none match.
string GetCollation()Returns the Collation for the current connection
string GetCollation(string database)Returns the Collation for the current connection
Task<string> GetCollationAsync()Returns the Collation for the current connection
Task<string> GetCollationAsync(string database)Returns the Collation for the current connection
Task<IReadOnlyList<Constraint>> GetConstraintsAsync(ISelectable selectable, int commandTimeout = null)Returns constraints for the given table. Empty list when the table is missing or has none (never null on success).
Task<IReadOnlyList<Constraint>> GetConstraintsAsync(int commandTimeout = null)Returns constraints in the database. Empty list when none exist (never null on success).
Task<IReadOnlyList<Constraint>> GetConstraintsAsync(string schema, int commandTimeout = null)Returns constraints filtered by schema. Empty list when the schema is missing or has none (never null on success).
Task<IReadOnlyList<IndexInformation>> GetIndexesAsync(int commandTimeout = null)Retrieve all indexes from the database. Empty list when none exist (never null on success).
Task<IReadOnlyList<IndexInformation>> GetIndexesAsync(string schema, int commandTimeout = null)Retrieve indexes from a specific database schema. Empty list when none exist (never null on success).
Task<IReadOnlyList<IndexInformation>> GetIndexesByTable(ITableInfo table, int commandTimeout = null)Retrieve indexes for a specific table. Empty list when none exist (never null on success).
Task<List<T>> GetListAsync(QueryBuilder<T> query, CancellationToken cancellationToken = null)Executes the specified query and returns a materialized list of results. Empty list when no rows match (never null on success).
Task<List<T>> GetListAsync(SelectStatement statement, CancellationToken cancellationToken = null)Executes a parameterized SelectStatement and returns a materialized list of results. Empty list when no rows match (never null on success).
Task<List<T>> GetListAsync(string statement, IReadOnlyList<IParameter> parameters = null, CancellationToken cancellationToken = null)Executes caller-provided SQL text and returns a materialized list of results. Empty list when no rows match (never null on success).
IAsyncEnumerable<T> GetListStream(QueryBuilder<T> query, CancellationToken cancellationToken = null)Executes the specified query and returns an asynchronous stream of results.
Type GetParameterType()Returns Type data for the connections underlying DbParameter implementation
int GetPid()Obtain the connections Process ID
Task<int> GetPidAsync()Obtain the connections Process ID
IReadOnlyCollection<string> GetRegisteredConnectionTypes()Returns a list of registered constructor types
TableInfo GetTableInfo(string table, string schema, bool includeIndexes, bool includeConstraints, int commandTimeout = null)Returns information about the specified database table
Task<IReadOnlyList<TableInfo>> GetTableInfoAsync(string schema, bool includeIndexes, bool includeConstraints, int commandTimeout = null)Returns information about the specified database table
Task<TableInfo> GetTableInfoAsync(ITableInfo tableInfo, bool includeIndexes, bool includeConstraints, int commandTimeout = null)Returns information about the specified database table
Task<TableInfo> GetTableInfoAsync(string table, string schema, bool includeIndexes, bool includeConstraints, int commandTimeout = null)Returns information about the specified database table
IReadOnlyList<string> GetTableList(int commandTimeout = null)Returns a List of tables in the current database
IReadOnlyList<string> GetTableList(string schema, int commandTimeout = null)Returns a List of tables limited by the schema name
Task<IReadOnlyList<string>> GetTableListAsync(string schema, int commandTimeout = null)Returns a List of tables limited by the schema name
string GetVersion()Returns version of the current database in string format
Task<string> GetVersionAsync()Returns version of the current database in string format
Task<bool> InsertAsync(T item, int commandTimeout = null, CancellationToken cancellationToken = null)Inserts a single dynamic row.
Task<bool> InsertAsync(TableInfo table, DynamicRow row, int commandTimeout = null, CancellationToken cancellationToken = null)Inserts a single dynamic row.
Task<bool> InsertAsync(Type entityType, object item, int commandTimeout = null, CancellationToken cancellationToken = null)Inserts a single dynamic row.
Task<int> InsertListAsync(IEnumerable<T> list, int commitInterval = null, bool useTransaction = null, int commandTimeout = null, CancellationToken cancellationToken = null)Inserts a list of dynamic rows using batched parameterized statements.
Task<int> InsertListAsync(TableInfo table, IEnumerable<DynamicRow> rows, int commitInterval = null, bool useTransaction = null, int commandTimeout = null, CancellationToken cancellationToken = null)Inserts a list of dynamic rows using batched parameterized statements.
Task<int> InsertListAsync(Type entityType, IEnumerable list, int commitInterval = null, bool useTransaction = null, int commandTimeout = null, CancellationToken cancellationToken = null)Inserts a list of dynamic rows using batched parameterized statements.
bool IsDisposed()Returns true/false based on if the connection has been Disposed()
bool IsRegistered(string dbType)Returns true if database type is registered for use
Task<Nullable<decimal>> MaxAsync(Expression<Func<T, object>> property, bool distinct = null, IPredicateNode predicate = null, CancellationToken cancellationToken = null)Computes the maximum value of the specified property for rows matching the optional predicate.
Task<TResult> MaxAsync(Expression<Func<T, TResult>> property, bool distinct = null, IPredicateNode predicate = null, CancellationToken cancellationToken = null)Computes the maximum value of the specified property for rows matching the optional predicate.
Task<Nullable<decimal>> MinAsync(Expression<Func<T, object>> property, bool distinct = null, IPredicateNode predicate = null, CancellationToken cancellationToken = null)Computes the minimum value of the specified property for rows matching the optional predicate.
Task<TResult> MinAsync(Expression<Func<T, TResult>> property, bool distinct = null, IPredicateNode predicate = null, CancellationToken cancellationToken = null)Computes the minimum value of the specified property for rows matching the optional predicate.
bool Open()Attempt to Open connection to the Database if it is not already in an Open state
Task<bool> OpenAsync(CancellationToken cancellationToken = null)Open the connection asynchronously if not already open.
QueryBuilder<T> Query()Creates a query builder for the specified entity type.
QueryBuilder<T> Query(Expression<Func<T, bool>> filter)Creates a query builder for the specified entity type with an initial WHERE filter.
bool RegisterConnectionType(string dbType)Use this to register custom connection implementations
bool RegisterConnectionType(string dbType, Type type)Use this to register custom connection implementations
bool RegisterDbProviderExtensions(DatabaseType dbType, IDbProviderExtensions provider)Register an extensions Provider fro advanced database activity
Task<bool> ReleaseDatabaseLockAsync(string resource, DatabaseLockMode mode, DatabaseLockOwner owner, CancellationToken cancellationToken = null)Releases one acquisition of a database-backed application lock.
void ReleaseSavepoint(string name)Releases a stored SAVEPOINT (noop on SQL Server)
void Rollback()Rollback current TRANSACTION
Task RollbackAsync()Rollback current TRANSACTION
void RollbackTo(string name)Rollback current TRANSACTION to a named SAVEPOINT
Task<bool> SaveAsync(T item, int commandTimeout = null, CancellationToken cancellationToken = null, UpsertConflictAction conflictAction = null)Alias for UpsertAsync.
Task<bool> SaveAsync(Type entityType, object item, int commandTimeout = null, CancellationToken cancellationToken = null, UpsertConflictAction conflictAction = null)Alias for the runtime-typed UpsertAsync overload.
Task<int> SaveListAsync(IEnumerable<T> list, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null, UpsertConflictAction conflictAction = null)Alias for UpsertListAsync.
Task<int> SaveListAsync(Type entityType, IEnumerable list, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null, UpsertConflictAction conflictAction = null)Alias for the runtime-typed UpsertListAsync overload.
void Savepoint(string name)Create a SAVEPOINT within the current TRANSACTION
Task<Nullable<decimal>> SumAsync(Expression<Func<T, object>> property, bool distinct = null, IPredicateNode predicate = null, CancellationToken cancellationToken = null)Computes the sum of the specified property for rows matching the optional predicate.
Task<TResult> SumAsync(Expression<Func<T, TResult>> property, bool distinct = null, IPredicateNode predicate = null, CancellationToken cancellationToken = null)Computes the sum of the specified property for rows matching the optional predicate.
bool Truncate()Executes a TRUNCATE against the underlying table for T.
bool Truncate(Type entityType)Executes a TRUNCATE against the underlying table for the specified runtime entity type.
Task<bool> TruncateAsync()Executes a TRUNCATE against the underlying table for T.
Task<bool> TruncateAsync(Type entityType)Executes a TRUNCATE against the underlying table for the specified runtime entity type.
Task<bool> UpdateAsync(T item, int commandTimeout = null, CancellationToken cancellationToken = null)Updates a single dynamic row by primary key.
Task<bool> UpdateAsync(TableInfo table, DynamicRow row, int commandTimeout = null, CancellationToken cancellationToken = null)Updates a single dynamic row by primary key.
Task<bool> UpdateAsync(Type entityType, object item, int commandTimeout = null, CancellationToken cancellationToken = null)Updates a single dynamic row by primary key.
Task<int> UpdateListAsync(IEnumerable<T> list, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null)Updates a list of dynamic rows by primary key using batched parameterized statements.
Task<int> UpdateListAsync(TableInfo table, IEnumerable<DynamicRow> rows, int commitInterval = null, bool useTransaction = null, int commandTimeout = null, CancellationToken cancellationToken = null)Updates a list of dynamic rows by primary key using batched parameterized statements.
Task<int> UpdateListAsync(Type entityType, IEnumerable list, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null)Updates a list of dynamic rows by primary key using batched parameterized statements.
Task<int> UpdateListAsync(UpdateSetBuilder<T> updateSet, int commandTimeout = null, CancellationToken cancellationToken = null)Updates a list of dynamic rows by primary key using batched parameterized statements.
Task<bool> UpsertAsync(T item, int commandTimeout = null, CancellationToken cancellationToken = null, UpsertConflictAction conflictAction = null)Inserts or updates a record depending on whether it already exists.
Task<bool> UpsertAsync(Type entityType, object item, int commandTimeout = null, CancellationToken cancellationToken = null, UpsertConflictAction conflictAction = null)Inserts or updates a record using a runtime entity type.
Task<int> UpsertListAsync(IEnumerable<T> list, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null, UpsertConflictAction conflictAction = null)Inserts or updates a list of records depending on whether each primary key already exists.
Task<int> UpsertListAsync(Type entityType, IEnumerable list, int commitInterval = null, int commandTimeout = null, CancellationToken cancellationToken = null, UpsertConflictAction conflictAction = null)Inserts or updates a list of records using a runtime entity type.