Docs Menu
Docs Home
/ / /
Entity Framework

Quick Start

On this page

  • Create a MongoDB Cluster
  • Set Up a Free Tier Cluster in Atlas
  • Update the Placeholders
  • Add Your Connection String to an Environment Variable
  • Set Up Your Project
  • Create the Project
  • Add the MongoDB Entity Framework Core Provider as a Dependency
  • Query Your MongoDB Cluster from Your Application
  • Add the Sample Code
  • Query the Sample Data
  • Next Steps

This guide shows you how to create a .NET application that uses the Entity Framework Core Provider to connect to a MongoDB Atlas cluster. If you prefer to connect to MongoDB using another programming language, see our list of official MongoDB drivers.

The Entity Framework Core Provider simplifies operations on data in MongoDB clusters by mapping the data to .NET objects.

MongoDB Atlas is a fully-managed cloud database service that hosts your data on MongoDB clusters. In this guide, we show you how to get started with your own free (no credit card required) cluster.

Follow the steps below to connect your Entity Framework Core Provider application to a MongoDB Atlas cluster.


To set up your Atlas free cluster required for this Quick Start, complete the guide MongoDB Atlas Setup guide.

After completing the steps in the Atlas guide, you have a new MongoDB cluster deployed in Atlas, a new database user, and sample datasets loaded into your cluster. You also have a connection string similar to the following in your copy buffer:


Paste the connection string in your copy buffer into a file in your preferred text editor. Replace the <username> and <password> placeholders with your database user's username and password.

Save this file to a safe location for use in the next step.


Run the following code in your shell to save the MongoDB connection string in your copy buffer from the previous step to an environment variable. Storing your connection string in an environment variable keeps your credentials separate from your source code. This separation makes it less likely to expose your credentials when sharing your code.


Create a new directory and use the dotnet new command to initialize your project as follows:

mkdir entity-quickstart
cd entity-quickstart
dotnet new console

Use the dotnet add command to add the Entity Framework Core Provider to your project as a dependency.

dotnet add package MongoDB.EntityFrameworkCore

Open the file named Program.cs in the base directory of your project. Copy the following sample code into Program.cs:

using Microsoft.EntityFrameworkCore;
using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.EntityFrameworkCore.Extensions;
var connectionString = Environment.GetEnvironmentVariable("MONGODB_URI");
if (connectionString == null)
Console.WriteLine("You must set your 'MONGODB_URI' environment variable. To learn how to set it, see");
var client = new MongoClient(connectionString);
var db = MflixDbContext.Create(client.GetDatabase("sample_mflix"));
var movie = db.Movies.First(m => m.title == "Back to the Future");
internal class MflixDbContext : DbContext
public DbSet<Movie> Movies { get; init; }
public static MflixDbContext Create(IMongoDatabase database) =>
new(new DbContextOptionsBuilder<MflixDbContext>()
.UseMongoDB(database.Client, database.DatabaseNamespace.DatabaseName)
public MflixDbContext(DbContextOptions options)
: base(options)
protected override void OnModelCreating(ModelBuilder modelBuilder)
internal class Movie
public ObjectId _id { get; set; }
public string title { get; set; }
public string rated { get; set; }
public string plot { get; set; }

Run the following command in your shell. It should print the plot of the movie "Back to the Future" from the sample dataset:

dotnet run entity-quickstart.csproj


If your output is empty, ensure you have loaded the sample datasets into your cluster.

After completing these steps, you should have a working Entity Framework application that connects to your MongoDB cluster, runs a query on the sample data, and prints out the result.

Learn how to use the Entity Framework Core Provider to perform common operations in Quick Reference.

← MongoDB Entity Framework Core Provider