Table of Contents

Class Db

Namespace
IceCoffee.Db4Net
Assembly
IceCoffee.Db4Net.dll

The main entrypoint for all data access methods of the library.

public static class Db
Inheritance
Db
Inherited Members

Properties

Registry

Gets the global database registry used by the static Db facade.

public static IDatabaseRegistry Registry { get; }

Property Value

IDatabaseRegistry

Methods

Configure(Db4NetSettings)

Configures the Db4Net library with custom settings.

public static void Configure(Db4NetSettings db4NetSettings)

Parameters

db4NetSettings Db4NetSettings

Custom settings for Db4Net.

CreateDatabaseRegistry()

Creates a new database registry instance.

public static IDatabaseRegistry CreateDatabaseRegistry()

Returns

IDatabaseRegistry

A new IDatabaseRegistry instance.

CreateDbConnection(string)

Creates a new database connection using the named context or the default context.

public static DbConnection CreateDbConnection(string databaseName = "")

Parameters

databaseName string

Optional. The name of the target database. If empty, the default context is used.

Returns

DbConnection

A new DbConnection instance.

Exceptions

InvalidOperationException

Thrown if the connection context is not initialized or if the specified context is not found.

CreateRepository(string)

Creates a new repository instance associated with the specified database name.

public static Repository CreateRepository(string databaseName = "")

Parameters

databaseName string

Optional. The name of the target database.

Returns

Repository

A new Repository instance.

CreateSqlAdapter(string)

Creates an SQL adapter implementation based on the provided or default database context.

public static ISqlAdapter CreateSqlAdapter(string databaseName = "")

Parameters

databaseName string

Optional. The name of the target database. If empty, the default context is used.

Returns

ISqlAdapter

A new IceCoffee.Db4Net.Core.SqlAdapters.ISqlAdapter instance.

Exceptions

InvalidOperationException

Thrown if the connection context is not initialized or if the specified context is not found.

CreateUnitOfWork(IsolationLevel, string)

Creates a new instance of the UnitOfWork class with the specified isolation level and optional database name.

public static UnitOfWork CreateUnitOfWork(IsolationLevel il, string databaseName = "")

Parameters

il IsolationLevel

The isolation level to be used for the transaction. This determines the locking and row versioning behavior during the transaction.

databaseName string

The name of the database to connect to. If not specified or empty, the default database connection is used.

Returns

UnitOfWork

A new UnitOfWork instance configured with the specified isolation level and database connection.

Remarks

Use this method to create a unit of work for managing database transactions. The isolation level controls how the transaction interacts with other concurrent transactions.

CreateUnitOfWork(string)

Creates a new instance of the UnitOfWork class, initialized with a database connection.

public static UnitOfWork CreateUnitOfWork(string databaseName = "")

Parameters

databaseName string

The name of the database to connect to. If not specified or an empty string is provided, the default database will be used.

Returns

UnitOfWork

A new UnitOfWork instance configured with the specified database connection.

Remarks

Use this method to create a unit of work for managing database operations. The connection is established based on the provided database name or defaults to the application's primary database if no name is specified.

DeleteMany<TEntity>(IEnumerable<TEntity>)

Creates a builder to delete multiple entities.

public static SqlDeleteEntityBuilder<TEntity> DeleteMany<TEntity>(IEnumerable<TEntity> entities)

Parameters

entities IEnumerable<TEntity>

A collection of entities to delete.

Returns

SqlDeleteEntityBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlDeleteEntityBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

DeleteMany<TEntity>(IEnumerable)

Creates a builder to delete multiple records by their identifiers for a specific entity type.

public static SqlDeleteBuilder<TEntity> DeleteMany<TEntity>(IEnumerable ids)

Parameters

ids IEnumerable

A collection of record identifiers.

Returns

SqlDeleteBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlDeleteBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

Delete<TEntity>()

Creates a builder to delete records for a specific entity type.

public static SqlDeleteBuilder<TEntity> Delete<TEntity>()

Returns

SqlDeleteBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlDeleteBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

Delete<TEntity>(object)

Creates a builder to delete a record by its identifier for a specific entity type.

public static SqlDeleteBuilder<TEntity> Delete<TEntity>(object id)

Parameters

id object

The record identifier.

Returns

SqlDeleteBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlDeleteBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

Delete<TEntity>(TEntity)

Creates a builder to delete a single entity.

public static SqlDeleteEntityBuilder<TEntity> Delete<TEntity>(TEntity entity)

Parameters

entity TEntity

The entity to delete.

Returns

SqlDeleteEntityBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlDeleteEntityBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

InsertAndGetId<TEntity>(TEntity)

Creates a builder to insert an entity and retrieve its generated identifier.

public static SqlInsertAndGetIdBuilder<TEntity> InsertAndGetId<TEntity>(TEntity entity)

Parameters

entity TEntity

The entity to insert.

Returns

SqlInsertAndGetIdBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlInsertAndGetIdBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

InsertMany<TEntity>(IEnumerable<TEntity>)

Creates a builder to insert multiple entity records.

public static SqlInsertEntityBuilder<TEntity> InsertMany<TEntity>(IEnumerable<TEntity> entities)

Parameters

entities IEnumerable<TEntity>

The entities to insert.

Returns

SqlInsertEntityBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlInsertEntityBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

InsertOrIgnoreMany<TEntity>(IEnumerable<TEntity>)

Creates a builder to insert multiple entities, ignoring conflicts for existing records.

public static SqlInsertOrIgnoreBuilder<TEntity> InsertOrIgnoreMany<TEntity>(IEnumerable<TEntity> entities)

Parameters

entities IEnumerable<TEntity>

The entities to insert.

Returns

SqlInsertOrIgnoreBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlInsertOrIgnoreBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

InsertOrIgnore<TEntity>(TEntity)

Creates a builder to insert an entity if it does not exist, ignoring conflicts otherwise.

public static SqlInsertOrIgnoreBuilder<TEntity> InsertOrIgnore<TEntity>(TEntity entity)

Parameters

entity TEntity

The entity to insert.

Returns

SqlInsertOrIgnoreBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlInsertOrIgnoreBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

InsertOrReplaceMany<TEntity>(IEnumerable<TEntity>)

Creates a builder to insert or replace multiple entities.

public static SqlInsertOrReplaceBuilder<TEntity> InsertOrReplaceMany<TEntity>(IEnumerable<TEntity> entities)

Parameters

entities IEnumerable<TEntity>

The entities to insert or replace.

Returns

SqlInsertOrReplaceBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlInsertOrReplaceBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

InsertOrReplace<TEntity>(TEntity)

Creates a builder to insert or replace an existing entity.

public static SqlInsertOrReplaceBuilder<TEntity> InsertOrReplace<TEntity>(TEntity entity)

Parameters

entity TEntity

The entity to insert or replace.

Returns

SqlInsertOrReplaceBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlInsertOrReplaceBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

Insert<TEntity>()

Creates a builder to insert new records for a specific entity type.

public static SqlInsertBuilder<TEntity> Insert<TEntity>()

Returns

SqlInsertBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlInsertBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

Insert<TEntity>(TEntity)

Creates a builder to insert a single entity record.

public static SqlInsertEntityBuilder<TEntity> Insert<TEntity>(TEntity entity)

Parameters

entity TEntity

The entity to insert.

Returns

SqlInsertEntityBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlInsertEntityBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

Query()

Creates a new IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryBuilder instance.

public static SqlQueryBuilder Query()

Returns

SqlQueryBuilder

A IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryBuilder object.

QueryCount<TEntity>()

Creates an SQL query builder for counting records in the default table of an entity type.

public static SqlQueryCountBuilder<TEntity> QueryCount<TEntity>()

Returns

SqlQueryCountBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryBuilder preconfigured to count.

Type Parameters

TEntity

The entity type.

QueryExists()

Creates a new IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryExistsBuilder to check for existence of records.

public static SqlQueryExistsBuilder QueryExists()

Returns

SqlQueryExistsBuilder

A IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryExistsBuilder object.

QueryExists<TEntity>()

Creates a new IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryExistsBuilder<TEntity> to check for existence of records for a specific entity type.

public static SqlQueryExistsBuilder<TEntity> QueryExists<TEntity>()

Returns

SqlQueryExistsBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryExistsBuilder<TEntity> object.

Type Parameters

TEntity

The entity type.

QueryExists<TEntity>(object)

Creates a new IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryExistsBuilder<TEntity> to check for existence of a record by its id.

public static SqlQueryExistsBuilder<TEntity> QueryExists<TEntity>(object id)

Parameters

id object

The record identifier.

Returns

SqlQueryExistsBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryExistsBuilder<TEntity> object.

Type Parameters

TEntity

The entity type.

QueryPaged()

Creates a new IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryPagedBuilder instance for paginated queries.

public static SqlQueryPagedBuilder QueryPaged()

Returns

SqlQueryPagedBuilder

A IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryPagedBuilder object.

QueryPaged(int, int)

Creates a new IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryPagedBuilder with specified page number and size.

public static SqlQueryPagedBuilder QueryPaged(int pageNumber, int pageSize)

Parameters

pageNumber int

The page number.

pageSize int

The number of items in each page.

Returns

SqlQueryPagedBuilder

A IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryPagedBuilder object.

QueryPaged<TEntity>()

Creates a paginated query builder for a specific entity type.

public static SqlQueryPagedBuilder<TEntity> QueryPaged<TEntity>()

Returns

SqlQueryPagedBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryPagedBuilder<TEntity> object.

Type Parameters

TEntity

The entity type.

QueryPaged<TEntity>(int, int)

Creates a paginated query builder for a specific entity type with explicit page number and size.

public static SqlQueryPagedBuilder<TEntity> QueryPaged<TEntity>(int pageNumber, int pageSize)

Parameters

pageNumber int

The page number.

pageSize int

The number of items per page.

Returns

SqlQueryPagedBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryPagedBuilder<TEntity> object.

Type Parameters

TEntity

The entity type.

QueryStoredProcedure(string, DynamicParameters)

Creates a stored procedure builder using Dapper.DynamicParameters.

public static SqlQueryStoredProcedureBuilder QueryStoredProcedure(string procName, DynamicParameters dynamicParameters)

Parameters

procName string

The name of the stored procedure.

dynamicParameters DynamicParameters

Dynamic parameters for the procedure.

Returns

SqlQueryStoredProcedureBuilder

An instance of IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryStoredProcedureBuilder.

QueryStoredProcedure(string, object?)

Creates a stored procedure builder with optional parameters.

public static SqlQueryStoredProcedureBuilder QueryStoredProcedure(string procName, object? param = null)

Parameters

procName string

The name of the stored procedure.

param object

Optional parameters for the procedure.

Returns

SqlQueryStoredProcedureBuilder

An instance of IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryStoredProcedureBuilder.

QueryStoredProcedure<TEntity>(string, DynamicParameters)

Creates a stored procedure builder using Dapper.DynamicParameters for a specific entity type.

public static SqlQueryStoredProcedureBuilder<TEntity> QueryStoredProcedure<TEntity>(string procName, DynamicParameters dynamicParameters)

Parameters

procName string

The name of the stored procedure.

dynamicParameters DynamicParameters

Dynamic parameters for the procedure.

Returns

SqlQueryStoredProcedureBuilder<TEntity>

An instance of IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryStoredProcedureBuilder<TEntity>.

Type Parameters

TEntity

The entity type.

QueryStoredProcedure<TEntity>(string, object?)

Creates a stored procedure builder with optional parameters for a specific entity type.

public static SqlQueryStoredProcedureBuilder<TEntity> QueryStoredProcedure<TEntity>(string procName, object? param = null)

Parameters

procName string

The name of the stored procedure.

param object

Optional parameters for the procedure.

Returns

SqlQueryStoredProcedureBuilder<TEntity>

An instance of IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryStoredProcedureBuilder<TEntity>.

Type Parameters

TEntity

The entity type.

Query<TEntity>()

Creates a new IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryBuilder<TEntity> for the specified entity type.

public static SqlQueryBuilder<TEntity> Query<TEntity>()

Returns

SqlQueryBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryBuilder<TEntity> object.

Type Parameters

TEntity

The entity type.

Query<TEntity>(object)

Creates a new IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryBuilder<TEntity> querying by a specific record id.

public static SqlQueryBuilder<TEntity> Query<TEntity>(object id)

Parameters

id object

The record identifier.

Returns

SqlQueryBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlQueryBuilder<TEntity> object.

Type Parameters

TEntity

The entity type.

Register(DatabaseProvider, string)

Registers a default database connection context with the specified provider and connection string.

public static void Register(DatabaseProvider databaseProvider, string connectionString)

Parameters

databaseProvider DatabaseProvider

The database provider type.

connectionString string

The connection string for the database.

Register(DbConnectionOptions)

Registers the default database connection context using the provided connection options. This default context will be used when no specific database name is specified.

public static void Register(DbConnectionOptions dbConnectionOptions)

Parameters

dbConnectionOptions DbConnectionOptions

The connection options containing the connection string and provider information.

Register(string, DbConnectionOptions)

Registers a database connection context for the specified database name.

public static void Register(string databaseName, DbConnectionOptions dbConnectionOptions)

Parameters

databaseName string

The name of the database to register.

dbConnectionOptions DbConnectionOptions

The connection options containing the connection string and provider information.

Exceptions

ArgumentException

Thrown when the provided database name is null or empty.

ArgumentNullException

Thrown when dbConnectionOptions is null.

RegisterMany(IEnumerable<KeyValuePair<string, DbConnectionOptions>>)

Registers multiple database connection contexts.

public static void RegisterMany(IEnumerable<KeyValuePair<string, DbConnectionOptions>> dbConnectionOptionsMap)

Parameters

dbConnectionOptionsMap IEnumerable<KeyValuePair<string, DbConnectionOptions>>

A collection mapping database names to their connection options.

UpdateMany<TEntity>(IEnumerable<TEntity>)

Creates a builder to update multiple entities.

public static SqlUpdateEntityBuilder<TEntity> UpdateMany<TEntity>(IEnumerable<TEntity> entities)

Parameters

entities IEnumerable<TEntity>

The entities to update.

Returns

SqlUpdateEntityBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlUpdateEntityBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

Update<TEntity>()

Creates a builder to update records for a specific entity type.

public static SqlUpdateBuilder<TEntity> Update<TEntity>()

Returns

SqlUpdateBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlUpdateBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.

Update<TEntity>(TEntity)

Creates a builder to update a single entity.

public static SqlUpdateEntityBuilder<TEntity> Update<TEntity>(TEntity entity)

Parameters

entity TEntity

The entity to update.

Returns

SqlUpdateEntityBuilder<TEntity>

A IceCoffee.Db4Net.Core.SqlBuilders.SqlUpdateEntityBuilder<TEntity> instance.

Type Parameters

TEntity

The entity type.