CoreEx.Database.SqlServer 4.0.0-preview-1

This is a prerelease version of CoreEx.Database.SqlServer.
dotnet add package CoreEx.Database.SqlServer --version 4.0.0-preview-1
                    
NuGet\Install-Package CoreEx.Database.SqlServer -Version 4.0.0-preview-1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="CoreEx.Database.SqlServer" Version="4.0.0-preview-1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CoreEx.Database.SqlServer" Version="4.0.0-preview-1" />
                    
Directory.Packages.props
<PackageReference Include="CoreEx.Database.SqlServer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add CoreEx.Database.SqlServer --version 4.0.0-preview-1
                    
#r "nuget: CoreEx.Database.SqlServer, 4.0.0-preview-1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package CoreEx.Database.SqlServer@4.0.0-preview-1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CoreEx.Database.SqlServer&version=4.0.0-preview-1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=CoreEx.Database.SqlServer&version=4.0.0-preview-1&prerelease
                    
Install as a Cake Tool

CoreEx.Database.SqlServer

Provides the SQL Server (Microsoft.Data.SqlClient) implementation of IDatabase: SqlServerDatabase, SqlServerUnitOfWork, session-context stamping, outbox relay, SQL Server-specific parameter extensions, and OpenTelemetry metrics for outbox operations.

Overview

CoreEx.Database.SqlServer is the SQL Server provider package built on top of CoreEx.Database. It supplies concrete implementations of every abstract type in that package — SqlServerDatabase, SqlServerCommand, SqlServerDatabaseArgs, SqlServerDatabaseColumns — and adds SQL Server-specific capabilities: session context propagation via sp_set_session_context, structured error-code mapping from SQL Server error numbers to CoreEx semantic exceptions, and SqlServerUnitOfWork for transactional outbox support.

The error-code mapping convention means stored procedures can raise well-known user error numbers (56001–56010) to signal domain exceptions without any application-layer switch statements. Duplicate-constraint violations (error numbers 2601, 2627 by default) are automatically converted to DuplicateException.

The outbox sub-namespace provides ready-to-use SqlServerOutboxPublisher, SqlServerOutboxRelay, and SqlServerOutboxRelayHostedService with .NET Meter-backed counters and histograms for enqueue throughput and relay lag.

Key capabilities

  • 🗃️ SqlServerDatabase: Concrete IDatabase for SqlConnection; maps SQL Server error numbers to CoreEx semantic exceptions; configurable DuplicateErrorNumbers; RowVersion encoded as Base64 string via StringBase64Converter.
  • 🔢 Error-code convention: SQL user error numbers 56001–56007 and 56010 map to ValidationException, BusinessException, AuthorizationException, ConcurrencyException, NotFoundException, ConflictException, DuplicateException, and DataConsistencyException respectively.
  • 👤 Session context: SetSqlSessionContextAsync(ExecutionContext?) invokes [dbo].[spSetSessionContext] (configurable) to stamp Username, Timestamp, TenantId, and UserId into the SQL Server session context for row-level security and audit triggers.
  • 🔁 SqlServerUnitOfWork: IDatabaseUnitOfWork implementation wrapping TransactionAsync with SqlServerUnitOfWorkInvoker; optionally accepts an IEventPublisher outbox for transactional event enqueuing.
  • 📤 Outbox relay: SqlServerOutboxPublisher (writes to outbox table), SqlServerOutboxRelay (polls and publishes), and SqlServerOutboxRelayHostedService (timer-driven hosted service) — all SQL Server-specific subclasses of the base CoreEx.Database.Outbox types.
  • 📊 Outbox metrics: SqlServerMetrics exposes .NET Meter instruments: sqlserver.outbox.enqueue (counter), sqlserver.outbox.relay.batch.size (counter), sqlserver.outbox.batch.oldest_lag and sqlserver.outbox.batch.newest_lag (histograms in ms).
  • 📡 OpenTelemetry: CoreExSqlServerExtensions.AddCoreExSqlServerOpenTelemetry wires SqlServerInvoker activity sources and the outbox meter into the OTEL tracer and meter providers.
  • ⚙️ DI registration: AddSqlServerDatabase<TDatabase>(services, configure?) registers SqlServerDatabase as a scoped service; AddSqlServerUnitOfWork(services, configure?) registers SqlServerUnitOfWork.

Key types

Type Description
SqlServerDatabase Concrete IDatabase for SqlConnection; error-number to exception mapping; RowVersionStringBase64Converter; SetSqlSessionContextAsync.
SqlServerUnitOfWork IDatabaseUnitOfWork implementation; wraps TransactionAsync with SqlServerUnitOfWorkInvoker; optional transactional outbox via IEventPublisher Outbox.
SqlServerCommand DatabaseCommand subclass for SqlCommand; adds SQL Server-specific parameter handling and CommandTimeout support.
SqlServerDatabaseArgs DatabaseArgs subclass with SQL Server-specific defaults (e.g. CommandTimeout).
SqlServerDatabaseColumns DatabaseColumns subclass adding SessionContextUsernameName, SessionContextTimestampName, SessionContextTenantIdName, SessionContextUserIdName constants for session-context parameters.
SqlServerMetrics Static .NET Meter with counters and histograms for outbox enqueue throughput and relay lag.
SqlServerOutboxPublisher DatabaseOutboxPublisherBase for SQL Server; inserts event rows into the outbox table via a configurable stored procedure.
SqlServerOutboxRelay DatabaseOutboxRelayBase<SqlServerDatabase, SqlServerOutboxRelay> with SQL Server-specific dequeue/complete SQL; records outbox metrics per batch.
SqlServerOutboxRelayHostedService DatabaseOutboxRelayHostedServiceBase<SqlServerOutboxRelay> timer-driven hosted service for the SQL Server outbox relay.
SqlServerExtensions IDatabase / DatabaseCommand extensions specific to SQL Server: Param helpers for SqlDbType-typed parameters and TVP (table-valued parameter) support.
SqlServerInvoker DatabaseInvoker subclass for SQL Server; the Default singleton used by SqlServerDatabase.
SqlServerUnitOfWorkInvoker InvokerBase<SqlServerUnitOfWork> wrapping TransactionAsync with OpenTelemetry spans and begin/commit/rollback log entries.
  • CoreEx.Database - Abstract base types (Database<TConn>, DatabaseCommand, DatabaseRecord, DatabaseOutboxRelayBase, IDatabaseMapper) that all SQL Server types extend.
  • CoreEx.Data - IUnitOfWork and IDatabaseUnitOfWork interfaces implemented by SqlServerUnitOfWork.
  • CoreEx.Events - IEventPublisher is the relay's publication target; SqlServerOutboxPublisher implements IEventPublisher.
  • CoreEx.Invokers - SqlServerInvoker and SqlServerUnitOfWorkInvoker extend InvokerBase for OpenTelemetry tracing.

Additional Resources

AI Usage Guide

An AGENTS.md file is included with this package. AI coding assistants (GitHub Copilot, Claude, Cursor, etc.) that support workspace-injected package documentation will automatically surface concise usage guidance, code examples, and Do Not rules for this package without requiring a local CoreEx checkout.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on CoreEx.Database.SqlServer:

Package Downloads
CoreEx.UnitTesting

Core .NET extensions and abstractions for the testing of backend services.

Beef.Database.SqlServer

Business Entity Execution Framework (Beef) SQL Server Database tool.

NTangle

NTangle Change-Data-Capture framework/runtime.

NTangle.CodeGen

NTangle Change-Data-Capture code-generation.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on CoreEx.Database.SqlServer:

Repository Stars
Avanade/Beef
The Business Entity Execution Framework (Beef) framework, and the underlying code generation, has been primarily created to support the industrialization of API development.
Version Downloads Last Updated
4.0.0-preview-1 82 6/20/2026
3.31.0 4,181 2/1/2025
3.30.2 269 12/11/2024
3.30.1 270 12/9/2024
3.30.0 1,805 11/21/2024
3.29.0 641 11/19/2024
3.28.0 287 11/9/2024
3.27.3 630 10/23/2024
3.27.2 257 10/17/2024
3.27.1 351 10/15/2024
3.27.0 517 10/11/2024
3.26.0 270 10/3/2024
3.25.6 561 10/2/2024
3.25.5 292 9/25/2024
3.25.4 342 9/24/2024
3.25.3 305 9/18/2024
3.25.2 285 9/17/2024
3.25.1 333 9/16/2024
3.25.0 311 9/10/2024
3.24.1 363 8/7/2024
Loading failed