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 EF 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 EF 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 EF 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 EF Core Provider application to a MongoDB Atlas cluster.
Create a MongoDB Cluster
Set Up a Free Tier Cluster in Atlas
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:
"mongodb+srv://<username>:<password>@cluster0.abc.mongodb.net/?retryWrites=true&w=majority"
Add Your Connection String to an Environment Variable
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.
export MONGODB_URI='<your connection string>'
Important
Make sure to replace the <username>
and <password>
sections of the connection
string with the username and password of your database user.
Set Up Your Project
Query Your MongoDB Cluster from Your Application
Add the Sample Code
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.Bson.Serialization.Attributes; 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 https://mongodb.prakticum-team.ru/docs/drivers/csharp/current/quick-start/#set-your-connection-string"); Environment.Exit(0); } 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"); Console.WriteLine(movie.Plot); public 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) .Options); public MflixDbContext(DbContextOptions options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<Movie>().ToCollection("movies"); } } public class Movie { [ ] public ObjectId _id { get; set; } [ ] public string Title { get; set; } [ ] public string Rated { get; set; } [ ] public string Plot { get; set; } }
Query the Sample Data
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
A young man is accidentally sent 30 years into the past in a time-traveling DeLorean invented by his friend, Dr. Emmett Brown, and must make sure his high-school-age parents unite in order to save his own existence.
Tip
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.
Next Steps
Learn how to use the EF Core Provider to perform common operations in Quick Reference.