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
                    
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="Net4x.DapperLibrary.MockSqlServer" Version="1.9.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Net4x.DapperLibrary.MockSqlServer" Version="1.9.9" />
                    
Directory.Packages.props
<PackageReference Include="Net4x.DapperLibrary.MockSqlServer" />
                    
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 Net4x.DapperLibrary.MockSqlServer --version 1.9.9
                    
#r "nuget: Net4x.DapperLibrary.MockSqlServer, 1.9.9"
                    
#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 Net4x.DapperLibrary.MockSqlServer@1.9.9
                    
#: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=Net4x.DapperLibrary.MockSqlServer&version=1.9.9
                    
Install as a Cake Addin
#tool nuget:?package=Net4x.DapperLibrary.MockSqlServer&version=1.9.9
                    
Install as a Cake Tool

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 .mdf database and return a configured IDapperContext.
  • Optionally reuse a single database file across tests via the ReuseDb flag to speed test runs.
  • Support for using Docker-based SQL Server containers on modern runtimes (Testcontainers) � enabled under NET6_0_OR_GREATER compilation.
  • Automatic configuration of DatabaseModel defaults 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 MockedSqlUseDapperContext instance.
    • Exposes GetInstance() overloads to obtain a ready-to-use IDapperContext (optionally accepting a logger and a custom path).
    • Exposes Use() and UseOnce() methods that populate the global DapperContextFactory ContextCreator so application code can request contexts via factory APIs.
    • Runtime-specific behavior: on modern runtimes it implements IDisposable / IAsyncDisposable to release Docker containers.
  • MockedSqlUseDapperContext

    • Internal helper that encapsulates the logic used by MockedSqlDapperContext to provision LocalDB instances or to fall back to Docker containers when requested.
    • Tracks flags such as ReuseDb, UseMaster and UseDocker to modify provisioning behavior.
  • LocalDbEnsurerInternal<T> and LocalDbInternal<T>

    • Responsible for creating and ensuring LocalDB instances exist, creating .mdf files, attaching/detaching databases, and returning an opened SqlConnection or IDapperContext rooted at the LocalDB instance.
    • LocalDbInternal contains helpers to create, drop and detach databases, and to map file paths used for created databases.

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 ReuseDb is disabled, the helper will attempt to clean up created .mdf and .ldf files when appropriate.
  • Use UseMaster to 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 UseDocker on supported runtimes to provision a SQL Server container instead.
  • Be mindful of permissions and available disk space when creating .mdf files.

Where to look in the codebase

  • DbContexts/MockedSqlDapperContext.* � the high-level context wrappers used by tests.
  • Internals/SqlServer/LocalDbEnsurerInternal.cs and LocalDbInternal.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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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