methodBeginDualTenantTransactionAsync
public static async Task<DbTransaction> BeginDualTenantTransactionAsync(this DbConnection connection, Guid companyId, Guid tenantId, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted, string companySessionVariable = DefaultCompanySessionVariable, string tenantSessionVariable = DefaultTenantSessionVariable, CancellationToken cancellationToken = default)
Opens a new transaction on the provided PostgreSQL connection and sets the dual-tenant RLS context atomically in a single roundtrip.
- connection: this DbConnection
- The database connection (opened automatically if not already open).
- companyId: Guid
- The company identifier.
- tenantId: Guid
- The tenant identifier.
- isolationLevel: IsolationLevel
- The transaction isolation level (default: `IsolationLevel.ReadCommitted`).
- companySessionVariable: string
- The PostgreSQL session variable name for company (default: `DefaultCompanySessionVariable`).
- tenantSessionVariable: string
- The PostgreSQL session variable name for tenant (default: `DefaultTenantSessionVariable`).
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation. The task result contains the started transaction with dual-tenant RLS context applied.
methodBeginDualTenantTransactionAsync
public static async Task<Npgsql.NpgsqlTransaction> BeginDualTenantTransactionAsync(this Npgsql.NpgsqlConnection connection, Guid companyId, Guid tenantId, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted, string companySessionVariable = DefaultCompanySessionVariable, string tenantSessionVariable = DefaultTenantSessionVariable, CancellationToken cancellationToken = default)
Opens a new transaction on the provided PostgreSQL connection and sets the dual-tenant RLS context atomically in a single roundtrip. Provides strongly-typed NpgsqlConnection support to eliminate ambiguity when multiple database provider packages are referenced.
- connection: this Npgsql.NpgsqlConnection
- companyId: Guid
- tenantId: Guid
- isolationLevel: IsolationLevel
- companySessionVariable: string
- tenantSessionVariable: string
- cancellationToken: CancellationToken
methodBeginTenantTransactionAsync
public static async Task<DbTransaction> BeginTenantTransactionAsync(this DbConnection connection, ITenantContext tenantContext, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted, string sessionVariable = DefaultTenantSessionVariable, CancellationToken cancellationToken = default)
Opens a new transaction on the provided PostgreSQL connection and sets the tenant RLS context atomically.
- connection: this DbConnection
- The database connection (opened automatically if not already open).
- tenantContext: ITenantContext
- The resolved tenant context.
- isolationLevel: IsolationLevel
- The transaction isolation level (default: `IsolationLevel.ReadCommitted`).
- sessionVariable: string
- The PostgreSQL session variable name for RLS.
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation. The task result contains the started transaction with RLS context applied.
methodBeginTenantTransactionAsync
public static async Task<Npgsql.NpgsqlTransaction> BeginTenantTransactionAsync(this Npgsql.NpgsqlConnection connection, ITenantContext tenantContext, IsolationLevel isolationLevel = IsolationLevel.ReadCommitted, string sessionVariable = DefaultTenantSessionVariable, CancellationToken cancellationToken = default)
Opens a new transaction on the provided PostgreSQL connection and sets the tenant RLS context atomically. Provides strongly-typed NpgsqlConnection support to eliminate ambiguity when multiple database provider packages are referenced.
- connection: this Npgsql.NpgsqlConnection
- The Npgsql database connection (opened automatically if not already open).
- tenantContext: ITenantContext
- The resolved tenant context.
- isolationLevel: IsolationLevel
- The transaction isolation level (default: `IsolationLevel.ReadCommitted`).
- sessionVariable: string
- The PostgreSQL session variable name for RLS.
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation. The task result contains the started transaction with RLS context applied.
methodResetRlsSessionContextAsync
public static async Task ResetRlsSessionContextAsync(this DbConnection connection, string companySessionVariable = DefaultCompanySessionVariable, string tenantSessionVariable = DefaultTenantSessionVariable, string bypassSessionVariable = DefaultBypassRlsSessionVariable, CancellationToken cancellationToken = default)
Scrubs session-level PostgreSQL Row Level Security GUC variables (`app.current_company_id`, `app.current_tenant_id`, and `app.bypass_rls`) to prevent cross-request connection pool contamination.
- connection: this DbConnection
- The open database connection.
- companySessionVariable: string
- The PostgreSQL session variable name for company.
- tenantSessionVariable: string
- The PostgreSQL session variable name for tenant.
- bypassSessionVariable: string
- The PostgreSQL session variable name for RLS bypass.
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetCompanyRlsContextAsync
public static async Task SetCompanyRlsContextAsync(this DbConnection connection, Guid companyId, bool isLocal = false, DbTransaction? transaction = null, string sessionVariable = DefaultCompanySessionVariable, bool failClosed = true, CancellationToken cancellationToken = default)
Establishes the company context for Row Level Security on an open database connection using `set_config`.
- connection: this DbConnection
- The database connection (opened automatically if not already open).
- companyId: Guid
- The company identifier.
- isLocal: bool
- `true` to scope the setting to the current transaction; `false` for session-level scope.
- transaction: DbTransaction?
- The optional active transaction.
- sessionVariable: string
- The PostgreSQL session variable name (defaults to `DefaultCompanySessionVariable`).
- failClosed: bool
- `true` to throw `TenantNotFoundException` if `companyId` is empty; `false` to set empty string.
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetCompanyRlsContextAsync
public static Task SetCompanyRlsContextAsync(this Npgsql.NpgsqlConnection connection, Guid companyId, bool isLocal = false, Npgsql.NpgsqlTransaction? transaction = null, string sessionVariable = DefaultCompanySessionVariable, bool failClosed = true, CancellationToken cancellationToken = default)
Establishes the company context for Row Level Security on an open NpgsqlConnection using `set_config`. Provides strongly-typed NpgsqlConnection support to eliminate ambiguity when multiple database provider packages are referenced.
- connection: this Npgsql.NpgsqlConnection
- The Npgsql database connection.
- companyId: Guid
- The company identifier.
- isLocal: bool
- `true` to scope the setting to the current transaction; `false` for session-level scope.
- transaction: Npgsql.NpgsqlTransaction?
- The optional active transaction.
- sessionVariable: string
- The PostgreSQL session variable name (defaults to `DefaultCompanySessionVariable`).
- failClosed: bool
- `true` to throw `TenantNotFoundException` if `companyId` is empty; `false` to set empty string.
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetDualTenantContextAsync
public static Task SetDualTenantContextAsync(this DbConnection connection, Guid companyId, Guid tenantId, CancellationToken cancellationToken = default)
Establishes the dual-tenant context (Company and Tenant) for Row Level Security on an open database connection in a single roundtrip command.
- connection: this DbConnection
- The database connection (opened automatically if not already open).
- companyId: Guid
- The company identifier.
- tenantId: Guid
- The tenant identifier.
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetDualTenantContextAsync
public static Task SetDualTenantContextAsync(this DbConnection connection, Guid companyId, Guid tenantId, DbTransaction? transaction, string companySessionVariable = DefaultCompanySessionVariable, string tenantSessionVariable = DefaultTenantSessionVariable, CancellationToken cancellationToken = default)
Establishes the dual-tenant context (Company and Tenant) for Row Level Security on an open database connection in a single roundtrip command.
- connection: this DbConnection
- The database connection (opened automatically if not already open).
- companyId: Guid
- The company identifier.
- tenantId: Guid
- The tenant identifier.
- transaction: DbTransaction?
- The optional active transaction within which `set_config` is executed.
- companySessionVariable: string
- The PostgreSQL session variable name for company (default: `DefaultCompanySessionVariable`).
- tenantSessionVariable: string
- The PostgreSQL session variable name for tenant (default: `DefaultTenantSessionVariable`).
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetDualTenantContextAsync
public static async Task SetDualTenantContextAsync(this DbConnection connection, Guid companyId, Guid tenantId, bool isLocal, DbTransaction? transaction = null, string companySessionVariable = DefaultCompanySessionVariable, string tenantSessionVariable = DefaultTenantSessionVariable, CancellationToken cancellationToken = default)
Establishes the dual-tenant context (Company and Tenant) for Row Level Security on an open database connection in a single roundtrip command.
- connection: this DbConnection
- The database connection (opened automatically if not already open).
- companyId: Guid
- The company identifier.
- tenantId: Guid
- The tenant identifier.
- isLocal: bool
- `true` to scope the variables locally to the current transaction; `false` for session-level scope.
- transaction: DbTransaction?
- The optional active transaction within which `set_config` is executed.
- companySessionVariable: string
- The PostgreSQL session variable name for company (default: `DefaultCompanySessionVariable`).
- tenantSessionVariable: string
- The PostgreSQL session variable name for tenant (default: `DefaultTenantSessionVariable`).
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetDualTenantContextAsync
public static Task SetDualTenantContextAsync(this Npgsql.NpgsqlConnection connection, Guid companyId, Guid tenantId, CancellationToken cancellationToken = default)
Establishes the dual-tenant context (Company and Tenant) for Row Level Security on an open NpgsqlConnection in a single roundtrip command. Provides strongly-typed NpgsqlConnection support to eliminate ambiguity when multiple database provider packages are referenced.
- connection: this Npgsql.NpgsqlConnection
- The Npgsql database connection.
- companyId: Guid
- The company identifier.
- tenantId: Guid
- The tenant identifier.
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetDualTenantContextAsync
public static Task SetDualTenantContextAsync(this Npgsql.NpgsqlConnection connection, Guid companyId, Guid tenantId, Npgsql.NpgsqlTransaction? transaction, string companySessionVariable = DefaultCompanySessionVariable, string tenantSessionVariable = DefaultTenantSessionVariable, CancellationToken cancellationToken = default)
Establishes the dual-tenant context (Company and Tenant) for Row Level Security on an open NpgsqlConnection within an active NpgsqlTransaction in a single roundtrip command. Provides strongly-typed NpgsqlConnection support to eliminate ambiguity when multiple database provider packages are referenced.
- connection: this Npgsql.NpgsqlConnection
- The Npgsql database connection.
- companyId: Guid
- The company identifier.
- tenantId: Guid
- The tenant identifier.
- transaction: Npgsql.NpgsqlTransaction?
- The active transaction within which `set_config` is executed.
- companySessionVariable: string
- The PostgreSQL session variable name for company (default: `DefaultCompanySessionVariable`).
- tenantSessionVariable: string
- The PostgreSQL session variable name for tenant (default: `DefaultTenantSessionVariable`).
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetDualTenantContextAsync
public static Task SetDualTenantContextAsync(this Npgsql.NpgsqlConnection connection, Guid companyId, Guid tenantId, bool isLocal, Npgsql.NpgsqlTransaction? transaction = null, string companySessionVariable = DefaultCompanySessionVariable, string tenantSessionVariable = DefaultTenantSessionVariable, CancellationToken cancellationToken = default)
Establishes the dual-tenant context (Company and Tenant) for Row Level Security on an open NpgsqlConnection within an optional NpgsqlTransaction in a single roundtrip command. Provides strongly-typed NpgsqlConnection support to eliminate ambiguity when multiple database provider packages are referenced.
- connection: this Npgsql.NpgsqlConnection
- The Npgsql database connection.
- companyId: Guid
- The company identifier.
- tenantId: Guid
- The tenant identifier.
- isLocal: bool
- `true` to scope the variables locally to the current transaction; `false` for session-level scope.
- transaction: Npgsql.NpgsqlTransaction?
- The optional active transaction within which `set_config` is executed.
- companySessionVariable: string
- The PostgreSQL session variable name for company (default: `DefaultCompanySessionVariable`).
- tenantSessionVariable: string
- The PostgreSQL session variable name for tenant (default: `DefaultTenantSessionVariable`).
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetRlsBypassContextAsync
public static async Task SetRlsBypassContextAsync(this DbConnection connection, bool bypassRls, DbTransaction? transaction = null, string bypassSessionVariable = DefaultBypassRlsSessionVariable, CancellationToken cancellationToken = default)
Configures the PostgreSQL Row Level Security bypass flag (`app.bypass_rls`) on the specified connection.
- connection: this DbConnection
- The database connection.
- bypassRls: bool
- Whether RLS policies should be bypassed for the current transaction or session.
- transaction: DbTransaction?
- Optional active transaction (when non-null, scopes the GUC to the transaction via `is_local = true`).
- bypassSessionVariable: string
- The PostgreSQL GUC variable name (defaults to `DefaultBypassRlsSessionVariable`).
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetScopedSessionContextAsync
public static async Task SetScopedSessionContextAsync(this DbConnection connection, string? tenantIdValue, string? userIdValue = null, string? sessionIdValue = null, bool isLocal = true, DbTransaction? transaction = null, string tenantSessionVariable = DefaultTenantSessionVariable, string userSessionVariable = DefaultUserSessionVariable, string sessionIdVariable = DefaultSessionIdVariable, CancellationToken cancellationToken = default)
Establishes scalar PostgreSQL Row Level Security GUC variables (tenant, user, and session identifiers) in a single roundtrip using `set_config`, supporting non-UUID scalar keys such as `BIGINT` or `TEXT`.
- connection: this DbConnection
- The database connection (opened automatically if not already open).
- tenantIdValue: string?
- The scalar tenant/company identifier string (or `null`/empty to clear).
- userIdValue: string?
- Optional scalar authenticated user identifier string.
- sessionIdValue: string?
- Optional scalar authenticated session identifier string.
- isLocal: bool
- `true` to scope the variables to the current transaction; `false` for session-level scope.
- transaction: DbTransaction?
- Optional active transaction within which `set_config` is executed.
- tenantSessionVariable: string
- The PostgreSQL GUC variable name for tenant (defaults to `DefaultTenantSessionVariable`).
- userSessionVariable: string
- The PostgreSQL GUC variable name for user (defaults to `DefaultUserSessionVariable`).
- sessionIdVariable: string
- The PostgreSQL GUC variable name for session (defaults to `DefaultSessionIdVariable`).
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetScopedSessionContextAsync
public static Task SetScopedSessionContextAsync(this Npgsql.NpgsqlConnection connection, string? tenantIdValue, string? userIdValue = null, string? sessionIdValue = null, bool isLocal = true, Npgsql.NpgsqlTransaction? transaction = null, string tenantSessionVariable = DefaultTenantSessionVariable, string userSessionVariable = DefaultUserSessionVariable, string sessionIdVariable = DefaultSessionIdVariable, CancellationToken cancellationToken = default)
Establishes scalar PostgreSQL Row Level Security GUC variables (tenant, user, and session identifiers) on an `Npgsql.NpgsqlConnection` in a single roundtrip using `set_config`.
- connection: this Npgsql.NpgsqlConnection
- The Npgsql database connection.
- tenantIdValue: string?
- The scalar tenant/company identifier string (or `null`/empty to clear).
- userIdValue: string?
- Optional scalar authenticated user identifier string.
- sessionIdValue: string?
- Optional scalar authenticated session identifier string.
- isLocal: bool
- `true` to scope the variables to the current transaction; `false` for session-level scope.
- transaction: Npgsql.NpgsqlTransaction?
- Optional active Npgsql transaction within which `set_config` is executed.
- tenantSessionVariable: string
- The PostgreSQL GUC variable name for tenant (defaults to `DefaultTenantSessionVariable`).
- userSessionVariable: string
- The PostgreSQL GUC variable name for user (defaults to `DefaultUserSessionVariable`).
- sessionIdVariable: string
- The PostgreSQL GUC variable name for session (defaults to `DefaultSessionIdVariable`).
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetTenantRlsContextAsync
public static Task SetTenantRlsContextAsync(this DbConnection connection, DbTransaction transaction, ITenantContext tenantContext, string sessionVariable = DefaultTenantSessionVariable, CancellationToken cancellationToken = default)
Establishes the tenant context for Row Level Security on an open database connection within the provided transaction.
- connection: this DbConnection
- The open database connection.
- transaction: DbTransaction
- The active transaction within which `SET LOCAL` is executed.
- tenantContext: ITenantContext
- The resolved tenant context containing the tenant identifier.
- sessionVariable: string
- The PostgreSQL session variable name. Defaults to `DefaultTenantSessionVariable`. Must match the variable referenced in RLS policies.
- cancellationToken: CancellationToken
- A token that can be used to cancel the asynchronous operation.
Returns: A task representing the asynchronous operation.
methodSetTenantRlsContextAsync
public static Task SetTenantRlsContextAsync(this Npgsql.NpgsqlConnection connection, Npgsql.NpgsqlTransaction transaction, ITenantContext tenantContext, string sessionVariable = DefaultTenantSessionVariable, CancellationToken cancellationToken = default)
Establishes the tenant context for Row Level Security on an open NpgsqlConnection within the provided transaction. Provides strongly-typed NpgsqlConnection support to eliminate ambiguity when multiple database provider packages are referenced.
- connection: this Npgsql.NpgsqlConnection
- transaction: Npgsql.NpgsqlTransaction
- tenantContext: ITenantContext
- sessionVariable: string
- cancellationToken: CancellationToken
fieldDefaultBypassRlsSessionVariable
public const string DefaultBypassRlsSessionVariable
Specifies the default PostgreSQL session configuration variable name used to bypass Row Level Security for platform operations.
fieldDefaultCompanySessionVariable
public const string DefaultCompanySessionVariable
Specifies the default PostgreSQL session configuration variable name used to set the current company.
fieldDefaultSessionIdVariable
public const string DefaultSessionIdVariable
Specifies the default PostgreSQL session configuration variable name used to set the current authenticated session identifier.
fieldDefaultTenantSessionVariable
public const string DefaultTenantSessionVariable
Specifies the default PostgreSQL session configuration variable name used to set the current tenant.
fieldDefaultUserSessionVariable
public const string DefaultUserSessionVariable
Specifies the default PostgreSQL session configuration variable name used to set the current authenticated user identifier.