Docs Menu
Docs Home
/ / /
EF Core Provider

Quick Reference

This page shows the provider syntax for several commands and links to their related API documentation.

Command
Syntax
Configure a DBContext

API Documentation
Fundamentals
public 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");
}
}
Create an Instance of the DBContext

// Replace the placeholder with your connection URI
var client = new MongoClient("<Your connection URI>");
var db = PlanetDbContext.Create(client.GetDatabase("sample_planets"));
db.Database.EnsureCreated();
var planet = db.Planets.FirstOrDefault(p => p.name == "Mercury");
Console.WriteLine(planet.name);
Find Multiple Entities

var planets = db.Planets.Where(p => p.hasRings == true);
foreach (var p in planets)
{
Console.WriteLine(p.name);
}
db.Planets.Add(new Planet()
{
name = "Pluto",
hasRings = false,
orderFromSun = 9
});
db.SaveChanges();
Insert Multiple Entities

var planets = new[]
{
new Planet()
{
_id = ObjectId.GenerateNewId(),
name = "Pluto",
hasRings = false,
orderFromSun = 9
},
new Planet()
{
_id = ObjectId.GenerateNewId(),
name = "Scadrial",
hasRings = false,
orderFromSun = 10
}
};
db.Planets.AddRange(planets);
db.SaveChanges();
var planet = db.Planets.FirstOrDefault(p => p.name == "Mercury");
planet.name = "Mercury the first planet";
db.SaveChanges();
Update Multiple Entities

var planets = db.Planets.Where(p => p.orderFromSun > 0);
foreach (var p in planets)
{
p.orderFromSun++;
}
db.SaveChanges();
var planet = db.Planets.FirstOrDefault(p => p.name == "Pluto");
db.Planets.Remove(planet);
db.SaveChanges();
Delete Multiple Entities

var pluto = db.Planets.FirstOrDefault(p => p.name == "Pluto");
var scadrial = db.Planets.FirstOrDefault(p => p.name == "Scadrial");
var planets = new[] { pluto, scadrial };
db.Planets.RemoveRange(planets);
db.SaveChanges();
Specify the Order in Which to Retrieve Entities

var planetList = db.Planets.OrderBy(p => p.orderFromSun);
foreach (var p in planetList)
{
Console.WriteLine(p.name);
}
Specify Multiple Orderings to Retrieve Entities

var planetList = db.Planets.OrderBy(o => o.hasRings).ThenBy(o => o.name);
foreach (var p in planetList)
{
Console.WriteLine("Has rings: " + p.hasRings + ", Name: " + p.name);
}
Specify the Number of Entities to Retrieve

var planetList = db.Planets.Take(3);
foreach (var p in planetList)
{
Console.WriteLine(p.name);
}
Specify the Number of Entities to Skip when Retrieving

var planetList = db.Planets.OrderBy(p => p.orderFromSun).Skip(5);
foreach (var p in planetList)
{
Console.WriteLine(p.name);
}

Back

Quick Start