BrandUp.MongoDB.Testing.Mongo2Go 10.0.12

dotnet add package BrandUp.MongoDB.Testing.Mongo2Go --version 10.0.12
                    
NuGet\Install-Package BrandUp.MongoDB.Testing.Mongo2Go -Version 10.0.12
                    
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="BrandUp.MongoDB.Testing.Mongo2Go" Version="10.0.12" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="BrandUp.MongoDB.Testing.Mongo2Go" Version="10.0.12" />
                    
Directory.Packages.props
<PackageReference Include="BrandUp.MongoDB.Testing.Mongo2Go" />
                    
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 BrandUp.MongoDB.Testing.Mongo2Go --version 10.0.12
                    
#r "nuget: BrandUp.MongoDB.Testing.Mongo2Go, 10.0.12"
                    
#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 BrandUp.MongoDB.Testing.Mongo2Go@10.0.12
                    
#: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=BrandUp.MongoDB.Testing.Mongo2Go&version=10.0.12
                    
Install as a Cake Addin
#tool nuget:?package=BrandUp.MongoDB.Testing.Mongo2Go&version=10.0.12
                    
Install as a Cake Tool

BrandUp.MongoDB

Build Status

A thin, DI-friendly layer on top of the official MongoDB.Driver that gives you EF-style database contexts, automatic collection registration, conventions, transactions, and ergonomic test helpers.

Installation

NuGet: BrandUp.MongoDB

dotnet add package BrandUp.MongoDB

Define a context

Declare a class deriving from MongoDbContext. Each IMongoCollection<TDocument> property is auto-registered as a collection. Mark every document with [MongoCollection].

using BrandUp.MongoDB;
using MongoDB.Driver;

public class WebSiteDbContext : MongoDbContext, ICommentsDbContext
{
    public IMongoCollection<ArticleDocument> Articles => GetCollection<ArticleDocument>();
    public IMongoCollection<CommentDocument> Comments => GetCollection<CommentDocument>();
}

public interface ICommentsDbContext
{
    IMongoCollection<CommentDocument> Comments { get; }
}

[MongoCollection(CollectionName = "Articles")]
public class ArticleDocument { /* ... */ }

[MongoCollection(CollectionName = "Comments")]
public class CommentDocument { /* ... */ }

Register with DI

services.AddMongoDb(options =>
{
    options.ConnectionString = "mongodb://localhost:27017";
});

services
    .AddMongoDbContext<WebSiteDbContext>(options =>
    {
        options.DatabaseName = "WebSite";
    })
    .AddExtension<WebSiteDbContext, ICommentsDbContext>()
    .UseCamelCaseElementName()
    .UseIgnoreIfNull(true)
    .UseIgnoreIfDefault(false);

Resolve and use

var dbContext = serviceProvider.GetRequiredService<WebSiteDbContext>();
var commentsDbContext = serviceProvider.GetRequiredService<ICommentsDbContext>();

await dbContext.Articles.InsertOneAsync(new ArticleDocument { /* ... */ });

Transactions (await using)

MongoDbSession is registered per DI scope. ITransactionFactory and IClientSessionHandle are exposed alongside it. The transaction handle implements both IDisposable and IAsyncDisposable, so prefer await using — that flows the rollback path through AbortTransactionAsync instead of blocking the thread.

using var scope = serviceProvider.CreateAsyncScope();
var dbContext = scope.ServiceProvider.GetRequiredService<WebSiteDbContext>();
var transactionFactory = scope.ServiceProvider.GetRequiredService<ITransactionFactory>();
var session = scope.ServiceProvider.GetRequiredService<IClientSessionHandle>();

await using var transaction = await transactionFactory.BeginAsync(ct);

await dbContext.Articles.InsertOneAsync(session, new ArticleDocument { /* ... */ }, cancellationToken: ct);

await transaction.CommitAsync(ct);
// If CommitAsync is not reached (exception, early return), DisposeAsync aborts the transaction.

Per-collection configuration

Tweak MongoCollectionSettings or CreateCollectionOptions for a specific document type without subclassing the metadata:

services
    .AddMongoDbContext<WebSiteDbContext>(options => options.DatabaseName = "WebSite")
    .ConfigureCollection<ArticleDocument>(
        configureSettings: s => s.ReadPreference = ReadPreference.SecondaryPreferred,
        configureCreate:   c => c.Capped = false);

The configureCreate hook only fires the first time the context boots against a fresh database — when the collection does not yet exist and is about to be created.

Testing

In-memory fakes — BrandUp.MongoDB.Testing

NuGet: BrandUp.MongoDB.Testing

services.AddFakeMongoDb();

Fast and dependency-free; no MongoDB process required. Suitable when you only need the in-memory shape of the driver API — many advanced operators (aggregation, change streams, full filter pipelines) are intentionally not implemented.

Real mongodBrandUp.MongoDB.Testing.EphemeralMongo

NuGet: BrandUp.MongoDB.Testing.EphemeralMongo

services.AddEphemeralMongoDb();

Spins up a real ephemeral mongod (single-node replica set, so transactions work) via EphemeralMongo. Pick this for integration tests where you want the actual driver behaviour.

Legacy — BrandUp.MongoDB.Testing.Mongo2Go (deprecated)

The Mongo2Go-backed helper is still published for backwards compatibility but is no longer maintained upstream. Both AddTestMongoDb() and Mongo2GoDbClientFactory are marked [Obsolete]; please migrate to AddEphemeralMongoDb().

Product Compatible and additional computed target framework versions.
.NET 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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
10.0.12 42 5/28/2026
10.0.9 94 5/20/2026
10.0.8 97 5/12/2026
10.0.7 98 5/12/2026
10.0.6 99 5/5/2026
10.0.5 114 4/27/2026
10.0.4 124 4/20/2026
10.0.3 179 3/16/2026
10.0.2 121 3/11/2026
10.0.1 178 1/3/2026
8.0.9 329 7/14/2025
8.0.8 505 6/11/2025
8.0.3 323 3/29/2025
8.0.2 371 3/9/2025
8.0.1 288 12/22/2024
7.3.2 363 7/9/2024
7.3.1 290 7/9/2024
7.2.5 323 6/30/2024
7.2.3 350 4/29/2024
7.1.5 300 4/29/2024
Loading failed