Configure the EF Core Provider
In this guide, you will learn how to configure an application to use the MongoDB Entity Framework Core Provider. To learn how to set up a new project and install the EF Core Provider, see the Quick Start.
Create a POCO
Create a Plain old CLR/Class object, or POCO, to use as a model for your entity. A POCO is a simple class object that doesn't inherit features from any framework-specific base classes or interfaces.
The following code example shows how to create a POCO that represents a customer:
public class Customer { public ObjectId Id { get; set; } public String Name { get; set; } public String Order { get; set; } }
Tip
To learn more about POCOs, see the POCO guide in the .NET/C# Driver documentation.
Create a DB Context Class
To begin using Entity Framework Core, create a context class that derives from
DBContext.
The DbContext
derived class instance represents a database session and is used to
query and save instances of your entities.
The DBContext
class exposes DBSet
properties that specify the entities you
can interact with while using that context.
The following example creates an instance of a DBContext
derived class and
specifies the Customer
object as a DBSet
property:
public class MyDbContext : DbContext { public DbSet<Customer> Customers { get; init; } public MyDbContext(DbContextOptions options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Customer>().ToCollection("customers"); } }
The previous code example overrides the OnModelCreating()
method. Overriding
the OnModelCreating()
method allows you to specify configuration details for your
model and its properties. This example uses the ToCollection()
method to
specify that the Customer
entities in your application map to the
customers
collection in MongoDB.
Use MongoDB
Once you've created a DBContext
class, construct a
DbContextOptionsBuilder
object and call its UseMongoDB()
method. This
method takes two parameters: a MongoClient
instance and
the name of the database that stores the collections you are working with.
The UseMongoDB()
method returns a DbContextOptions
object. Pass the
Options
property of this object to the constructor for your DBContext
class.
The following example shows how to construct a DBContext
object in
this way:
var mongoClient = new MongoClient("<Your MongoDB Connection URI>"); var dbContextOptions = new DbContextOptionsBuilder<MyDbContext>().UseMongoDB(mongoClient, "<Database Name"); var db = new MyDbContext(dbContextOptions.Options);
Tip
Creating a MongoClient
You can call methods from the MongoDB .NET/C# Driver when using
the EF Core Provider. The previous example uses the
MongoClient()
method from the .NET/C# Driver to create a MongoDB
client that connects to a MongoDB instance.
To learn more about using the MongoDB .NET/C# Driver to connect to MongoDB, see the Connection guide in the .NET/C# Driver documentation.
Example
The following code example shows how to configure the EF Core Provider and insert a document into the database:
using Microsoft.EntityFrameworkCore; using MongoDB.Bson; using MongoDB.Driver; using Microsoft.Extensions.Configuration; using MongoDB.EntityFrameworkCore.Extensions; var mongoClient = new MongoClient("<Your MongoDB Connection URI>"); var dbContextOptions = new DbContextOptionsBuilder<MyDbContext>().UseMongoDB(mongoClient, "<Database Name>"); var db = new MyDbContext(dbContextOptions.Options); // Add a new customer and save it to the database db.Customers.Add(new Customer() { name = "John Doe", Order = "1 Green Tea" }); db.SaveChanges(); public class Customer { public ObjectId Id { get; set; } public String Name { get; set; } public String Order { get; set; } } public class MyDbContext : DbContext { public DbSet<Customer> Customers { get; init; } public MyDbContext(DbContextOptions options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Customer>().ToCollection("customers"); } }