This page contains frequently asked questions and their corresponding answers.
Tip
If you can't find an answer to your problem on this page, see the Issues & Help page for next steps and more resources.
What is the Difference Between the .NET/C# Driver and the EF Core Provider?
The MongoDB .NET/C# Driver is a library that exposes MongoDB functionality directly and includes a LINQ provider with projections, group operations, and flexible mapping. The driver includes features such as the following:
Transactions
Bulk operations
LINQ queries
Operations that directly modify the database
Aggregation operations
Custom mapping
The EF Core Provider allows you to use Microsoft's Entity Framework Core with MongoDB in your .NET/C# applications. The EF Core Provider supports change tracking, entity-based LINQ operations, and modeling familiar to Entity Framework Core users. The provider includes features such as the following:
Intelligent object tracking
Entity-based LINQ operations
Entity Framework modeling and mapping with the fluent API
Automatic database updates through change tracking
How Can I Manage Indexes with the EF Core Provider?
You can create indexes with the EF Core Provider by calling the HasIndex()
method in the OnModelCreating() method of your DbContext class. To learn
more about how to create indexes with the EF Core Provider, see the
Indexes guide.
Because the EF Core Provider is built on top of the .NET/C# Driver,
you can also manage indexes in your application by using the .NET/C# Driver
directly. To use driver methods in your EF Core Provider
application, call them on the MongoClient used to set up your
DbContext.
The following example creates indexes on the movies collection by using
.NET/C# Driver methods:
using MongoDB.Driver; var client = new MongoClient("<connection string>"); var database = client.GetDatabase("sample_mflix"); await CreateIndexesAsync(database); async Task CreateIndexesAsync(IMongoDatabase database) { var moviesIndex = new CreateIndexModel<Movie>(Builders<Movie>.IndexKeys .Ascending(x => x.Title) .Ascending(x => x.Genres)); await database.GetCollection<Movie>("movies") .Indexes.CreateOneAsync(moviesIndex); }
To learn more about creating indexes by using the driver, see the Indexes guide in the MongoDB .NET/C# Driver documentation.
Can I Access .NET/C# Driver Features in the EF Core Provider?
You can use the .NET/C# Driver directly in your EF Core Provider
application by calling driver methods on the MongoClient used to set up
your DbContext. The EF Core Provider is built on top of the
.NET/C# Driver, so you can use all driver features in your
application. For example, you can use the driver to perform an Atlas Search
query as shown in the following example:
// Client used to set up your DbContext var client = new MongoClient("<connection string>"); var clientDB = client.GetDatabase("sample_guides"); var collection = clientDB.GetCollection<Planet>("planets"); var searchResult = collection.Aggregate() .Search(Builders<Planet>.Search.Equals(p => p.hasRings, true)) .ToList(); foreach (var p in searchResult) { Console.WriteLine(p.name); }
To learn more about performing Atlas Search queries with the .NET/C# Driver, see the Atlas Search guide in the driver documentation.