![]() |
VOOZH | about |
dotnet add package BrandUp.MongoDB.Testing.EphemeralMongo --version 10.0.12
NuGet\Install-Package BrandUp.MongoDB.Testing.EphemeralMongo -Version 10.0.12
<PackageReference Include="BrandUp.MongoDB.Testing.EphemeralMongo" Version="10.0.12" />
<PackageVersion Include="BrandUp.MongoDB.Testing.EphemeralMongo" Version="10.0.12" />Directory.Packages.props
<PackageReference Include="BrandUp.MongoDB.Testing.EphemeralMongo" />Project file
paket add BrandUp.MongoDB.Testing.EphemeralMongo --version 10.0.12
#r "nuget: BrandUp.MongoDB.Testing.EphemeralMongo, 10.0.12"
#:package BrandUp.MongoDB.Testing.EphemeralMongo@10.0.12
#addin nuget:?package=BrandUp.MongoDB.Testing.EphemeralMongo&version=10.0.12Install as a Cake Addin
#tool nuget:?package=BrandUp.MongoDB.Testing.EphemeralMongo&version=10.0.12Install as a Cake Tool
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.
NuGet: BrandUp.MongoDB
dotnet add package BrandUp.MongoDB
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 { /* ... */ }
services.AddMongoDb(options =>
{
options.ConnectionString = "mongodb://localhost:27017";
});
services
.AddMongoDbContext<WebSiteDbContext>(options =>
{
options.DatabaseName = "WebSite";
})
.AddExtension<WebSiteDbContext, ICommentsDbContext>()
.UseCamelCaseElementName()
.UseIgnoreIfNull(true)
.UseIgnoreIfDefault(false);
var dbContext = serviceProvider.GetRequiredService<WebSiteDbContext>();
var commentsDbContext = serviceProvider.GetRequiredService<ICommentsDbContext>();
await dbContext.Articles.InsertOneAsync(new ArticleDocument { /* ... */ });
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.
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.
BrandUp.MongoDB.TestingNuGet: 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.
mongod — BrandUp.MongoDB.Testing.EphemeralMongoNuGet: 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.
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 | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
This package is not used by any NuGet packages.
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 10.0.12 | 112 | 5/28/2026 |