MVFC.Outbox.MongoDB.EntityFrameworkCore
5.0.0
See the version list below for details.
dotnet add package MVFC.Outbox.MongoDB.EntityFrameworkCore --version 5.0.0
NuGet\Install-Package MVFC.Outbox.MongoDB.EntityFrameworkCore -Version 5.0.0
<PackageReference Include="MVFC.Outbox.MongoDB.EntityFrameworkCore" Version="5.0.0" />
<PackageVersion Include="MVFC.Outbox.MongoDB.EntityFrameworkCore" Version="5.0.0" />
<PackageReference Include="MVFC.Outbox.MongoDB.EntityFrameworkCore" />
paket add MVFC.Outbox.MongoDB.EntityFrameworkCore --version 5.0.0
#r "nuget: MVFC.Outbox.MongoDB.EntityFrameworkCore, 5.0.0"
#:package MVFC.Outbox.MongoDB.EntityFrameworkCore@5.0.0
#addin nuget:?package=MVFC.Outbox.MongoDB.EntityFrameworkCore&version=5.0.0
#tool nuget:?package=MVFC.Outbox.MongoDB.EntityFrameworkCore&version=5.0.0
MVFC.Outbox.MongoDB.EntityFrameworkCore
Stages MongoDB Outbox records through EF Core in the application's transaction. Uses the native MongoDB driver for worker operations with the shared BSON mapping, and for collection and index initialization.
Supports .NET 10 and .NET 11. Both targets use EF Core 10 for compatibility with the MongoDB EF Core provider.
dotnet add package MVFC.Outbox.MongoDB.EntityFrameworkCore
ProducerOnly saves messages; ConsumerProducer also starts the worker. Manual dispatch uses the same confirmation flow (IOutboxExecutionPersistence). For custom registration updates, see the 4.0.0 migration guide.
Configuration and usage
These example variables come from your application: services, configuration, connection, ct, order and topicId. Register your DbContext with UseMongoDB and map model.AddOutbox().ToCollection("outbox") in OnModelCreating.
Use matching values for ConnectionString, DatabaseName and CollectionName in UseMongoDB and UseMongoDBEntityFramework.
using MVFC.Outbox.EntityFrameworkCore;
using MVFC.Outbox.EntityFrameworkCore.Extensions;
using MVFC.Outbox.Extensions;
using MVFC.Outbox.MongoDB.EntityFrameworkCore;
using MVFC.Outbox.MongoDB.EntityFrameworkCore.Extensions;
using MVFC.Outbox.MongoDB.Extensions;
using MVFC.Outbox.Publishing;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using MongoDB.EntityFrameworkCore.Extensions;
using MongoDB.EntityFrameworkCore.Metadata;
services.AddDbContext<ApplicationDbContext>(db => db.UseMongoDB(connection, "orders"));
services.AddOutbox(configuration, outbox => outbox.ProducerOnly()
.MapAggregate<Order, Guid>(order => order.Id, "order")
.UseMongoDBEntityFramework<ApplicationDbContext>(mongo =>
{
mongo.ConnectionString = connection;
mongo.DatabaseName = "orders";
mongo.CollectionName = "outbox";
}));
// Same scoped ApplicationDbContext used by the publisher:
await using var transaction = await database.Database.BeginTransactionAsync(ct);
database.Orders.Add(order);
await publisher.For(order).To(topicId).EnqueueAsync(ct);
await database.SaveChangesAsync(ct);
await transaction.CommitAsync(ct);
public sealed class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: DbContext(options)
{
public DbSet<Order> Orders => Set<Order>();
protected override void OnModelCreating(ModelBuilder model)
{
model.Entity<Order>().HasKey(order => order.Id);
model.Entity<Order>().ToCollection("orders");
model.AddOutbox().ToCollection("outbox");
}
}
public sealed record Order(Guid Id, decimal Total);
Runtime & transactions:
EnqueueAsyncreturnsStaged; callSaveChangesAsyncand commit the transaction to save the messages. Worker operations use native conditional writes with the shared BSON mapping. Custom mappings retain EF reads and versioned saves, as described below.MongoDB transactions require a replica set or sharded cluster.
Without a context type:
UseMongoDBEntityFramework(mongo => ...)creates a separate Outbox-onlyDbContextand does not share transactions with another business context.
Dedicated index initialization and existing collections
When AddOutbox(...UseMongoDBEntityFramework...) is registered, resolve IOutboxSchemaInitializer directly. To initialize the schema in a separate step, without configuring topics or starting a worker, use AddMongoDBEntityFrameworkOutboxSchema:
using MVFC.Outbox.MongoDB.EntityFrameworkCore;
using MVFC.Outbox.MongoDB.EntityFrameworkCore.Extensions;
using MVFC.Outbox.Persistence;
services.AddMongoDBEntityFrameworkOutboxSchema(mongo =>
{
mongo.ConnectionString = connection;
mongo.DatabaseName = "orders";
mongo.CollectionName = "outbox";
});
await using var scope = provider.CreateAsyncScope();
await scope.ServiceProvider.GetRequiredService<IOutboxSchemaInitializer>().InitializeAsync(ct);
InitializeAsyncuses the native MongoDB driver to create the Outbox collection and indexes, including the TTL index onExpiresAtUtcwithexpireAfterSeconds: 0. Manage business collections separately.Documents without
ProjectIdorAttributesJsonremain readable. For documents created beforeDestinationKind(Transport = 0,Handler = 1), fill in the missing field before deployment:
db.outbox.updateMany(
{ DestinationKind: { $exists: false } },
{ $set: { DestinationKind: 0 } }
);
Worker operations and custom mappings
| Stage | Behavior |
|---|---|
| Publication | Stages messages in the application's EF context. SaveChangesAsync and commit persist business data and the Outbox together. |
| Worker with standard mapping | Uses the native Driver to atomically reserve, renew, complete and record failures, without saving pending application entities. |
| Worker with custom mapping | Uses the EF adapter for renamed fields, converters, BSON representations, shadow properties or global filters. Selection is automatic; the execution flow stays the same. |
The collection name comes from the EF model. Configure the same database and collection in EF and schema initialization. Preserve the numeric status and version contracts when customizing storage.
Run initialization to add ix_outbox_ready and ix_outbox_retry. Custom physical fields require equivalent indexes managed by the application. These mappings do not use the native optimization measured in the benchmark.
Requirements
An application targeting .NET 10 or .NET 11.
A MongoDB EF Core DbContext with the Outbox mapping. Transactional publication requires MongoDB configured as a replica set or sharded cluster.
For development, use the .NET 11 SDK in global.json and .NET 10 targeting support. The playground and integration tests also require Docker. See the playground guide.
Contributing
See CONTRIBUTING.md for setup, Cake builds, tests and coverage conventions. Dependencies are listed in Directory.Packages.props.
License
Distributed under the Apache License 2.0. See LICENSE for all terms: permissions, redistribution conditions, and limits on warranty and liability.
| Product | Versions 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. net11.0 is compatible. |
-
net10.0
- Microsoft.EntityFrameworkCore (>= 10.0.15)
- Microsoft.EntityFrameworkCore.Relational (>= 10.0.15)
- Microsoft.Extensions.Hosting (>= 10.0.15)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 10.0.15)
- MongoDB.Driver (>= 3.14.1)
- MongoDB.EntityFrameworkCore (>= 10.0.6)
- MVFC.Outbox.EntityFrameworkCore (>= 5.0.0)
- MVFC.Outbox.MongoDB (>= 5.0.0)
-
net11.0
- Microsoft.EntityFrameworkCore (>= 10.0.15)
- Microsoft.EntityFrameworkCore.Relational (>= 10.0.15)
- Microsoft.Extensions.Hosting (>= 11.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 11.0.0)
- MongoDB.Driver (>= 3.14.1)
- MongoDB.EntityFrameworkCore (>= 10.0.6)
- MVFC.Outbox.EntityFrameworkCore (>= 5.0.0)
- MVFC.Outbox.MongoDB (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.