MongoDbCore 2.0.0
dotnet add package MongoDbCore --version 2.0.0
NuGet\Install-Package MongoDbCore -Version 2.0.0
<PackageReference Include="MongoDbCore" Version="2.0.0" />
<PackageVersion Include="MongoDbCore" Version="2.0.0" />
<PackageReference Include="MongoDbCore" />
paket add MongoDbCore --version 2.0.0
#r "nuget: MongoDbCore, 2.0.0"
#:package MongoDbCore@2.0.0
#addin nuget:?package=MongoDbCore&version=2.0.0
#tool nuget:?package=MongoDbCore&version=2.0.0
MongoDbCore
A lightweight micro-ORM for MongoDB on .NET 8 and .NET 9. Built on top of
MongoDB.Driver 2.x with an EF-Core-flavoured surface: a MongoDbContext
that exposes typed Collection<T> properties, conventional collection naming,
Include/IncludeRef eager loading, optional auditing, and an in-memory
SelfCachedCollection<T> for read-heavy workloads.
Install
dotnet add package MongoDbCore
Quick start
// appsettings.json
"MongoDB": {
"Connection": "mongodb://localhost:27017",
"Database": "myapp",
"MaxConnectionPoolSize": 100,
"EnableAuditing": false
}
// Program.cs
builder.Services.AddMongoDbContext<AppDbContext>(
builder.Configuration.GetSection("MongoDB").Get<MongoDbCoreOptions>()!);
// AppDbContext.cs
public class AppDbContext(MongoDbCoreOptions options) : MongoDbContext(options)
{
public Collection<Todo> Todos { get; set; } = null!;
public SelfCachedCollection<Country> Countries { get; set; } = null!;
protected override Task OnInitializedAsync()
{
// seed indexes / initial data here
return Task.CompletedTask;
}
}
// Todo.cs
public class Todo : BaseEntity
{
public string Task { get; set; } = string.Empty;
public bool IsDone { get; set; }
}
// usage
public class TodoService(AppDbContext db)
{
public Task<List<Todo>> ListAsync(CancellationToken ct) =>
db.Todos.ToListAsync(ct);
public Task<Todo> AddAsync(string task, CancellationToken ct) =>
db.Todos.AddAsync(new Todo { Task = task }, ct);
public Task<Todo?> GetAsync(string id, CancellationToken ct) =>
db.Todos.FirstOrDefaultAsync(t => t.Id == id, ct);
public Task<Todo> UpdateAsync(Todo t, CancellationToken ct) =>
db.Todos.UpdateAsync(t, ct);
public Task DeleteAsync(string id, CancellationToken ct) =>
db.Todos.DeleteAsync(id, ct);
}
Collection naming
By default, the collection name is the pluralized snake-case form of the
entity type name. Todo → todos. TodoItem → todo_items.
Relationships — Include and IncludeRef
// Book has a CategoryId pointing to a Category by ObjectId.
public class Book : BaseEntity
{
public string Title { get; set; } = string.Empty;
[ReferenceTo(nameof(Category))] public string CategoryId { get; set; } = string.Empty;
public Category? Category { get; set; }
}
public class Category : BaseEntity
{
public string Name { get; set; } = string.Empty;
[ForeignKeyTo(nameof(Book))] public List<Book> Books { get; set; } = [];
}
// Forward (parent → children):
var category = db.Categories.Include(c => c.Books).FirstOrDefault(c => c.Id == id);
// Backward (child → parent):
var book = db.Books.IncludeRef(b => b.Category).FirstOrDefault(b => b.Id == id);
Auditing (opt-in)
// 1) Enable
new MongoDbCoreOptions { EnableAuditing = true, ... };
// 2) Mark the entity
[Auditable]
public class Invoice : BaseEntity { /* ... */ }
// 3) Add/Update/Delete is automatically logged to the audits collection.
// 4) Resolve IAuditService to inspect:
public class AuditController(IAuditService audits) : ControllerBase
{
public Task<(List<AuditEntity>, long)> GetAll(int page = 1)
=> audits.GetAllAsync(pageSize: 20, page: page);
}
SelfCachedCollection<T> — read-heavy lookups
public SelfCachedCollection<Country> Countries { get; set; } = null!;
// Reads come from an in-memory snapshot; writes go through MongoDB and
// refresh the cache under a lock. Eager loading (Include) isn't supported.
var country = db.Countries.FirstOrDefault(c => c.Code == "UZ");
What's new in 2.0
See CHANGELOG.md. Highlights: the package finally compiles
(the Audit / AuditEntity mismatch made 1.x unusable from a fresh restore),
auditing is opt-in, UTC timestamps, server-side Select, multi-target
net8.0;net9.0, dropped Blazor dependency.
Building from source
dotnet restore MongoDbCore.sln
dotnet build MongoDbCore.sln --configuration Release
dotnet test tests/MongoDbCore.Tests/MongoDbCore.Tests.csproj --configuration Release
License
MIT — see LICENSE.
Contributing
PRs welcome. See CONTRIBUTING.md.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | 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 is compatible. 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 was computed. 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. |
-
net8.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- MongoDB.Driver (>= 2.30.0)
-
net9.0
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.2)
- MongoDB.Driver (>= 2.30.0)
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 |
|---|---|---|
| 2.0.0 | 80 | 5/18/2026 |
| 1.4.1 | 137 | 1/9/2025 |
| 1.4.0 | 93 | 1/9/2025 |
| 1.3.0 | 165 | 10/8/2024 |
| 1.2.0 | 118 | 8/5/2024 |
| 1.1.0-alpha | 118 | 7/24/2024 |
| 1.0.9-alpha | 118 | 7/19/2024 |
| 1.0.8-alpha | 104 | 7/18/2024 |
| 1.0.7-alpha | 115 | 7/16/2024 |
| 1.0.6-alpha | 110 | 7/16/2024 |
| 1.0.5-alpha | 112 | 7/16/2024 |
| 1.0.4-alpha | 106 | 7/12/2024 |
| 1.0.3-alpha | 115 | 7/11/2024 |
| 1.0.2-alpha | 100 | 7/10/2024 |
| 1.0.1 | 124 | 8/2/2024 |
| 1.0.1-alpha | 107 | 7/9/2024 |
| 1.0.0 | 126 | 8/1/2024 |
| 1.0.0-alpha | 123 | 7/2/2024 |
2.0.0 — major cleanup: fixed Audit class mismatch, UTC timestamps, proper async init, removed Blazor dependency, multi-target net8/net9, removed reflection-driven Console logging.