Menu Docs
Página inicial do Docs
/ / /
Provedor do EF Core

Referência rápida

Esta página mostra a sintaxe do provedor para vários comandos e links para a documentação de API relacionada.

Comando
Sintaxe
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"));
var planet = db.Planets.FirstOrDefault(p => p.name == "Mercury");
Console.WriteLine(planet.name);
Mercury
Find Multiple Entities

var planets = db.Planets.Where(p => p.hasRings == true);
foreach (var p in planets)
{
Console.WriteLine(p.name);
}
Neptune
Uranus
Saturn
Jupiter
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);
}
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
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);
}
Has rings: False, Name: Earth
Has rings: False, Name: Mars
Has rings: False, Name: Mercury
Has rings: False, Name: Venus
Has rings: True, Name: Jupiter
Has rings: True, Name: Neptune
Has rings: True, Name: Saturn
Has rings: True, Name: Uranus
Specify the Number of Entities to Retrieve

var planetList = db.Planets.Take(3);
foreach (var p in planetList)
{
Console.WriteLine(p.name);
}
Neptune
Mercury
Mars
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);
}
Saturn
Uranus
Neptune

Voltar

Início rápido