![]() |
VOOZH | about |
dotnet add package MongoDB.EntityFrameworkCore --version 10.0.2
NuGet\Install-Package MongoDB.EntityFrameworkCore -Version 10.0.2
<PackageReference Include="MongoDB.EntityFrameworkCore" Version="10.0.2" />
<PackageVersion Include="MongoDB.EntityFrameworkCore" Version="10.0.2" />Directory.Packages.props
<PackageReference Include="MongoDB.EntityFrameworkCore" />Project file
paket add MongoDB.EntityFrameworkCore --version 10.0.2
#r "nuget: MongoDB.EntityFrameworkCore, 10.0.2"
#:package MongoDB.EntityFrameworkCore@10.0.2
#addin nuget:?package=MongoDB.EntityFrameworkCore&version=10.0.2Install as a Cake Addin
#tool nuget:?package=MongoDB.EntityFrameworkCore&version=10.0.2Install as a Cake Tool
The MongoDB EF Core Provider enables MongoDB interaction with Entity Framework Core 8 or 9 on .NET 8.0 or later, and Entity Framework Core 10 on .NET 10.0 or later.
It supports MongoDB database server 5.0 or later, preferably in a transaction-enabled configuration.
First, create a DbContext with the desired entities and configuration:
internal class PlanetDbContext : DbContext
{
public DbSet<Planet> Planets { get; init; }
public static PlanetDbContext Create(IMongoDatabase database) =>
new(new DbContextOptionsBuilder<PlanetDbContext>()
.UseMongoDB(database.Client, database.DatabaseNamespace.DatabaseName)
.Options);
public PlanetDbContext(DbContextOptions options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Planet>().ToCollection("planets");
}
}
var mongoConnectionString = Environment.GetEnvironmentVariable("MONGODB_URI");
var mongoClient = new MongoClient(mongoConnectionString);
var db = PlanetDbContext.Create(mongoClient.GetDatabase("planets"));
db.Database.EnsureCreated();
var planet = db.Planets.FirstOrDefault(x => x.Name == "Earth");
var mongoConnectionString = builder.Configuration.GetConnectionString("MongoDbUri")!;
builder.Services.AddDbContext<PlanetDbContext>(options =>
{
options.UseMongoDB(mongoConnectionString, DatabaseName);
});
If you need some more configuration, you can create the MongoClient and inject it into the DbContext:
var mongoConnectionString = builder.Configuration.GetConnectionString("MongoDbUri")!;
var mongoUrl = new MongoUrl(mongoConnectionString);
var mongoClient = new MongoClient(mongoUrl);
builder.Services.AddSingleton<IMongoClient>(mongoClient);
builder.Services.AddDbContext<PlanetDbContext>((provider, options) =>
{
var client = provider.GetRequiredService<IMongoClient>();
options.UseMongoDB(client, DatabaseName);
});
Later on, simply inject PlanetDbContext where it's needed and continue as you would normally.
Entity Framework Core and MongoDB have a wide variety of features. This provider supports a subset of the functionality available in both, specifically:
Where, Find, First, Single, OrderBy, ThenBy, Skip, Take etc.VectorSearch extension method on DbSet and fluent vector index configurationAny, Count, LongCount, Sum, Min, Max, Average, All[Column] or [BsonElement] attributes or HasElementName("name") method[Table("name")], ToCollection("name") or by convention from the DbSet property nameGuid and ObjectId etc.int, string, Guid, decimal, DateOnly etc.) & MongoDB types (ObjectId, Decimal128)byte[]BsonIgnore, BsonId, BsonDateTimeOptions, BsonElement, BsonRepresentation and BsonRequired supportEnsureCreated & EnsureDeleted to ensure collections and the database created at app start-upIsConcurrencyToken/ConcurrencyCheckAttribute & IsRowVersion/TimestampAttributeSaveChanges & SaveChangesAsync - all changes committed or rolled-back togetherCamelCaseElementNameConvention for helping map Pascal-cased C# properties to camel-cased BSON elementsOfType<T> and Where(e => e is T)A number of Entity Framework Core features are not currently supported but planned for future release. If you require use of these facilities in the mean-time consider using the existing MongoDB C# Driver's LINQ provider which may support them.
This project's version-numbers are aligned with Entity Framework Core and as-such we can not use the semver convention of constraining breaking changes solely to major version numbers. Please keep an eye on our document before upgrading to a new version of this provider.
If you've identified a security vulnerability in a driver or any other MongoDB project, please report it according to the instructions here.
Please see our for contributing to the driver.
Thank you to everyone who has contributed to this project.
| 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. |
Showing the top 5 NuGet packages that depend on MongoDB.EntityFrameworkCore:
| Package | Downloads |
|---|---|
|
ShayganTadbir.Framework.Core
Package description |
|
|
Genocs.Persistence.EFCore
EF Core repository and persistence integration for Genocs applications. |
|
|
Myth.Testing
Testing utilities, mocks, and base test classes for comprehensive unit and integration testing. Includes Bogus data generation, FluentAssertions, Moq, xUnit, and Entity Framework in-memory testing support. |
|
|
eQuantic.Core.Data.EntityFramework.MongoDb
Core Data library for Entity Framework and Mongo DB |
|
|
ThingsLibrary.Entity.Mongo
Things Library entity Mongo (NoSQL) libraries and wrappers. |
Showing the top 6 popular GitHub repositories that depend on MongoDB.EntityFrameworkCore:
| Repository | Stars |
|---|---|
|
microsoft/aspire
Aspire is the tool for code-first, extensible, observable dev and deploy.
|
|
|
BrighterCommand/Brighter
A framework for building messaging apps with .NET and C#.
|
|
|
AlphaYu/adnc
A pragmatic .NET 8 framework for modular monoliths to evolve seamlessly into distributed microservices.
|
|
|
netcorepal/netcorepal-cloud-framework
一个基于ASP.NET Core实现的整洁领域驱动设计落地战术框架。 A tactical framework for Clean Domain-Driven Design based on ASP.NET Core.
|
|
|
thisisnabi/Shortener
Shortener Url as a service in ASP.NET Core and Actor Model
|
|
|
thisisnabi/Notify
Notify as a service in ASP.NET Core & VSA
|
| Version | Downloads | Last Updated |
|---|---|---|
| 10.0.2 | 4,348 | 6/3/2026 |
| 10.0.1 | 71,579 | 3/11/2026 |
| 10.0.0 | 34,520 | 2/6/2026 |
| 9.1.2 | 203 | 6/3/2026 |
| 9.1.1 | 5,390 | 3/11/2026 |
| 9.1.0 | 2,920 | 2/5/2026 |
| 9.0.4 | 16,477 | 1/20/2026 |
| 9.0.3 | 81,029 | 10/24/2025 |
| 9.0.2 | 10,320 | 10/10/2025 |
| 9.0.1 | 42,349 | 8/29/2025 |
| 9.0.0 | 168,211 | 4/16/2025 |
| 9.0.0-preview.1 | 7,567 | 3/7/2025 |
| 8.4.2 | 503 | 6/3/2026 |
| 8.4.1 | 4,372 | 3/11/2026 |
| 8.4.0 | 3,787 | 2/6/2026 |
| 8.3.4 | 2,223 | 1/20/2026 |
| 8.3.3 | 32,650 | 10/24/2025 |
| 8.3.2 | 8,667 | 10/10/2025 |
| 8.3.1 | 18,145 | 8/28/2025 |
| 8.3.0 | 69,979 | 4/16/2025 |