Net4x.DapperLibrary.MockSqlServer
1.9.9
dotnet add package Net4x.DapperLibrary.MockSqlServer --version 1.9.9
NuGet\Install-Package Net4x.DapperLibrary.MockSqlServer -Version 1.9.9
<PackageReference Include="Net4x.DapperLibrary.MockSqlServer" Version="1.9.9" />
<PackageVersion Include="Net4x.DapperLibrary.MockSqlServer" Version="1.9.9" />
<PackageReference Include="Net4x.DapperLibrary.MockSqlServer" />
paket add Net4x.DapperLibrary.MockSqlServer --version 1.9.9
#r "nuget: Net4x.DapperLibrary.MockSqlServer, 1.9.9"
#:package Net4x.DapperLibrary.MockSqlServer@1.9.9
#addin nuget:?package=Net4x.DapperLibrary.MockSqlServer&version=1.9.9
#tool nuget:?package=Net4x.DapperLibrary.MockSqlServer&version=1.9.9
DapperLibrary.MockSqlServer
Lightweight helpers to create and use a mocked SQL Server IDapperContext for tests and local development. This project provides a small framework around LocalDB (and optional Docker-based SQL Server containers on modern runtimes) to obtain IDapperContext instances backed by a file-based or ephemeral SQL Server instance.
Purpose
Make it easy to spin up a disposable or reusable SQL Server instance for unit and integration tests and to obtain a corresponding IDapperContext preconfigured with SQL Server defaults used by the DapperLibraries.
Features
- Create or attach a LocalDB
.mdfdatabase and return a configuredIDapperContext. - Optionally reuse a single database file across tests via the
ReuseDbflag to speed test runs. - Support for using Docker-based SQL Server containers on modern runtimes (Testcontainers) � enabled under
NET6_0_OR_GREATERcompilation. - Automatic configuration of
DatabaseModeldefaults for SQL Server (identity initializer and datetime literal formatting). - Helpers to create/drop/attach/detach databases programmatically.
Key types
MockedSqlDapperContext/MockedSqlDapperContext<TSqlDapperContext>- Primary entry point for consumers. Provides convenience static methods and properties that wrap an internal
MockedSqlUseDapperContextinstance. - Exposes
GetInstance()overloads to obtain a ready-to-useIDapperContext(optionally accepting a logger and a custom path). - Exposes
Use()andUseOnce()methods that populate the globalDapperContextFactoryContextCreatorso application code can request contexts via factory APIs. - Runtime-specific behavior: on modern runtimes it implements
IDisposable/IAsyncDisposableto release Docker containers.
- Primary entry point for consumers. Provides convenience static methods and properties that wrap an internal
MockedSqlUseDapperContext- Internal helper that encapsulates the logic used by
MockedSqlDapperContextto provision LocalDB instances or to fall back to Docker containers when requested. - Tracks flags such as
ReuseDb,UseMasterandUseDockerto modify provisioning behavior.
- Internal helper that encapsulates the logic used by
LocalDbEnsurerInternal<T>andLocalDbInternal<T>- Responsible for creating and ensuring LocalDB instances exist, creating
.mdffiles, attaching/detaching databases, and returning an openedSqlConnectionorIDapperContextrooted at the LocalDB instance. LocalDbInternalcontains helpers to create, drop and detach databases, and to map file paths used for created databases.
- Responsible for creating and ensuring LocalDB instances exist, creating
Usage examples
Obtain a context for tests:
// Simple: get an IDapperContext backed by LocalDB
using var context = MockedSqlDapperContext.GetInstance();
// Get instance with a logger
var ctx = MockedSqlDapperContext.GetInstance(myLogger);
// Get instance using a specific file path
var ctx2 = MockedSqlDapperContext.GetInstance(myLogger, "C:\\temp\\testdb.mdf");
Control reuse and lifecycle:
// When true, the same .mdf file may be reused across calls to speed up tests
MockedSqlDapperContext.ReuseDb = true;
// When false, each call will dispose any reused context and create a fresh database
MockedSqlDapperContext.ReuseDb = false;
// When false, a non-master database file will be used per GetInstance() call
MockedSqlDapperContext.UseMaster = false;
Register context creation for code that consumes the DapperContextFactory:
// Register the context creator so code calling DapperContextFactory.Instance.CreateContext() gets a mocked context
MockedSqlDapperContext.Use();
// Register once (adds a disposal handler)
MockedSqlDapperContext.UseOnce();
Docker support (modern runtimes only):
// On .NET 6+ you can opt into using Testcontainers if LocalDB is not available
MockedSqlDapperContext.UseDapperContext.UseDocker = true;
var ctx = MockedSqlDapperContext.GetInstance();
Multi-targeting notes
- The project includes conditional compilation to support multiple frameworks. LocalDB-based helpers target .NET Framework and older runtimes; Docker-based Testcontainers support is compiled under
NET6_0_OR_GREATER. - The repository contains different build outputs for net40/net45/net461 and modern targets; pick the appropriate compiled asset for your application runtime.
Behavior & best practices
- The code aims to avoid leaving dangling LocalDB files: when
ReuseDbis disabled, the helper will attempt to clean up created.mdfand.ldffiles when appropriate. - Use
UseMasterto control whether created databases are attached to the LocalDB master or created as separate files. - For reliable integration tests in CI (where LocalDB may not be present), enable
UseDockeron supported runtimes to provision a SQL Server container instead. - Be mindful of permissions and available disk space when creating
.mdffiles.
Where to look in the codebase
DbContexts/MockedSqlDapperContext.*� the high-level context wrappers used by tests.Internals/SqlServer/LocalDbEnsurerInternal.csandLocalDbInternal.cs� LocalDB provisioning, creation, attach/detach and helper functions.Internals/MockedSqlUseDapperContext.cs� the glue between high-level APIs and the LocalDB/Docker provisioning logic.
This README summarizes usage and integration points. Inspect the source code and XML docs for detailed implementation and further customization.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. 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 was computed. 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. |
| .NET Framework | net40 is compatible. net403 was computed. net45 is compatible. net451 was computed. net452 was computed. net46 was computed. net461 is compatible. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
-
.NETFramework 4.0
- Net4x.DapperLibrary.Base (>= 1.9.9)
- Net4x.DapperLibrary.Installation (>= 1.9.9)
- Net4x.DapperLibrary.SqlServer (>= 1.9.9)
-
.NETFramework 4.5
- Net4x.DapperLibrary.Base (>= 1.9.9)
- Net4x.DapperLibrary.Installation (>= 1.9.9)
- Net4x.DapperLibrary.SqlServer (>= 1.9.9)
-
.NETFramework 4.6.1
- Net4x.DapperLibrary.Base (>= 1.9.9)
- Net4x.DapperLibrary.Installation (>= 1.9.9)
- Net4x.DapperLibrary.SqlServer (>= 1.9.9)
- System.Configuration.ConfigurationManager (>= 9.0.0)
-
net10.0
- Microsoft.Data.SqlClient (>= 5.2.1)
- Microsoft.Extensions.Logging (>= 10.0.0)
- Net4x.DapperLibrary.Base (>= 1.9.9)
- Net4x.DapperLibrary.Installation (>= 1.9.9)
- Net4x.DapperLibrary.SqlServer (>= 1.9.9)
-
net6.0
- Microsoft.Data.SqlClient (>= 5.2.1)
- Microsoft.Extensions.Logging (>= 10.0.0)
- Net4x.DapperLibrary.Base (>= 1.9.9)
- Net4x.DapperLibrary.Installation (>= 1.9.9)
- Net4x.DapperLibrary.SqlServer (>= 1.9.9)
-
net8.0
- Microsoft.Data.SqlClient (>= 5.2.1)
- Microsoft.Extensions.Logging (>= 10.0.0)
- Net4x.DapperLibrary.Base (>= 1.9.9)
- Net4x.DapperLibrary.Installation (>= 1.9.9)
- Net4x.DapperLibrary.SqlServer (>= 1.9.9)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Net4x.DapperLibrary.MockSqlServer:
| Package | Downloads |
|---|---|
|
Net4x.Xsd2Db.Base
Package Description |
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.9.9 | 0 | 12/22/2025 |
| 1.6.0.12 | 118 | 12/12/2025 |
| 1.6.0.11 | 109 | 12/12/2025 |
| 1.6.0.10 | 424 | 12/9/2025 |
| 1.6.0.9 | 198 | 12/4/2025 |
| 1.6.0.8 | 185 | 12/4/2025 |
| 1.6.0.7 | 249 | 11/30/2025 |
| 1.6.0.6 | 174 | 11/27/2025 |
| 1.6.0.5 | 235 | 11/22/2025 |
| 1.6.0.4 | 136 | 11/16/2025 |
| 1.6.0.3 | 175 | 11/15/2025 |
| 1.6.0.2 | 266 | 11/14/2025 |
| 1.6.0.1 | 246 | 11/10/2025 |
| 1.6.0 | 207 | 11/9/2025 |
| 1.5.0.3 | 199 | 11/5/2025 |
| 1.5.0.2 | 200 | 11/3/2025 |
| 1.5.0.1 | 202 | 11/3/2025 |
| 1.5.0 | 166 | 10/26/2025 |
| 1.4.1.6 | 183 | 10/24/2025 |
| 1.4.1.5 | 188 | 10/22/2025 |
| 1.4.1.4 | 207 | 10/22/2025 |
| 1.4.1.2 | 210 | 5/30/2025 |
| 1.4.1.1 | 212 | 4/30/2025 |
| 1.4.1 | 206 | 4/30/2025 |
| 1.4.0 | 253 | 3/31/2025 |
| 1.1.0 | 785 | 8/26/2023 |
| 1.1.0-at20230506041203 | 747 | 5/7/2023 |