Docs / MooDbContext Reference

MooDbContext Reference

Role#

MooDbContext is the main entry point for stored procedure execution, transactions, SQL text access, bulk loading, scalar reads, and result mapping.

Constructors#

</> C#
public MooDbContext(string connectionString, MooDbContextOptions? options = null)
public MooDbContext(SqlConnection connection, MooDbContextOptions? options = null)

Properties#

  • Sql : MooSql
  • Bulk : MooBulk

Main methods#

</> C#
Task<int> ExecuteAsync(...)
Task<T> ScalarAsync<T>(...)
Task<T?> SingleAsync<T>(...)
Task<T?> SingleAsync<T>(..., Func<SqlDataReader, T> map, ...)
Task<List<T>> ListAsync<T>(...)
Task<List<T>> ListAsync<T>(..., Func<SqlDataReader, T> map, ...)
Task<TResult> QueryMultipleAsync<TResult>(..., Func<IMooMultiReader, TResult> read, ...)
Task<MooTransaction> BeginTransactionAsync(IsolationLevel isolationLevel = IsolationLevel.ReadCommitted, ...)

Key points#

  • the main surface is stored procedure based
  • raw SQL goes through Sql
  • bulk loading goes through Bulk
  • transactions start here

Lifetime and disposal#

MooDbContext is not disposable and should not be wrapped in using or await using.

When created with a connection string, MooDbContext opens and disposes connections internally for each operation.

When you start a transaction using BeginTransactionAsync(), the returned MooTransaction becomes the unit of work. If the transaction owns its connection, that connection is disposed when the transaction is disposed.

When created with an existing SqlConnection, the caller retains ownership of that connection and remains responsible for disposing it.