MongoDB.EntityFrameworkCore 9.0.0

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

MongoDB Entity Framework Core Provider

MongoDB.EntityFrameworkCore

The MongoDB EF Core Provider requires Entity Framework Core 8 or 9 on .NET 8 or later and a MongoDB database server 5.0 or later, preferably in a transaction-enabled configuration.

Getting Started

Setup a DbContext with your 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");
    }
}

To get going with the DbContext:

var mongoConnectionString = Environment.GetEnvironmentVariable("MONGODB_URI");
var mongoClient = new MongoClient(mongoConnectionString);
var db = PlanetDbContext.Create(mongoClient.GetDatabase("planets"));
db.Database.EnsureCreated();

Supported Features

Entity Framework Core and MongoDB have a wide variety of features. This provider supports a subset of the functionality available in both, specifically:

  • Querying with Where, Find, First, Single, OrderBy, ThenBy, Skip, Take etc.
  • Top-level aggregate Any, Count, LongCount, Sum, Min, Max, Average, All
  • Mapping properties to BSON elements using [Column] or [BsonElement] attributes or HasElementName("name") method
  • Mapping entities to collections via [Table("name")], ToCollection("name") or by convention from the DbSet property name
  • Single or composite keys of standard types including string, Guid and ObjectId etc.
  • Properties with typical CLR types (int, string, Guid, decimal, DateOnly etc.) & MongoDB types (ObjectId, Decimal128)
  • Properties that are arrays, lists, dictionaries (string keys) of simple CLR types including binary byte[]
  • Owned entities (aka value types, sub-documents, embedded documents) both directly and in collection properties
  • BsonIgnore, BsonId, BsonDateTimeOptions, BsonElement, BsonRepresentation and BsonRequired support
  • Storage type configuration through EF ValueConverters or BSON representation attributes and fluent APIs
  • Query and update logging of MQL (sensitive logging must be enabled)
  • EnsureCreated & EnsureDeleted to ensure collections and the database created at app start-up
  • Optimistic concurrency support through IsConcurrencyToken/ConcurrencyCheckAttribute & IsRowVersion/TimestampAttribute
  • AutoTransactional SaveChanges & SaveChangesAsync - all changes committed or rolled-back together
  • CamelCaseElementNameConvention for helping map Pascal-cased C# properties to camel-cased BSON elements
  • Type discriminators including OfType<T> and Where(e => e is T)
  • Support for EF shadow properties and EF.Proxy for navigation traversal
  • Client Side Field Level Encryption and Queryable Encryption compatibility

Limitations

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.

Planned for future releases

  • Select projections with client-side operations
  • GroupBy operations
  • Includes/joins
  • Geospatial
  • Atlas search
  • ExecuteUpdate & ExecuteDelete bulk operations (EF 9 only)

Not supported, out-of-scope features

  • Keyless entity types
  • Schema migrations
  • Database-first & model-first
  • Alternate keys
  • Document (table) splitting
  • Temporal tables
  • Timeseries
  • GridFS

Breaking changes

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 Breaking Changes document before upgrading to a new version of this provider.

Documentation

Questions/Bug Reports

If you’ve identified a security vulnerability in a driver or any other MongoDB project, please report it according to the instructions here.

Contributing

Please see our guidelines for contributing to the driver.

Thank you to everyone who has contributed to this project.

Product 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 was computed.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (12)

Showing the top 5 NuGet packages that depend on MongoDB.EntityFrameworkCore:

Package Downloads
ShayganTadbir.Framework.Core

Package description

eQuantic.Core.Data.EntityFramework.MongoDb

Core Data library for Entity Framework and Mongo DB

Adnc.Infra.Repository.EfCore.MongoDB

该工程是在`Adnc.Infra.Repository.EfCore`基础上MongoDB的实现。

xSdk.Data.EntityFramework.MongoDB

Package Description

Chieratto.SDK.Infrastructure.MongoDB

Package Description

GitHub repositories (2)

Showing the top 2 popular GitHub repositories that depend on MongoDB.EntityFrameworkCore:

Repository Stars
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
9.0.0 559 4/16/2025
9.0.0-preview.1 1,573 3/7/2025
8.3.0 265 4/16/2025
8.2.3 33,068 2/20/2025
8.2.2 11,660 2/5/2025
8.2.1 46,230 12/13/2024
8.2.0 46,596 10/28/2024
8.1.1 71,577 9/10/2024
8.1.0 48,687 8/15/2024
8.0.3 68,769 6/28/2024
8.0.2 9,775 6/19/2024
8.0.1 47,609 5/13/2024
8.0.0 20,547 4/30/2024
7.0.0-preview.1 54,305 10/11/2023