LINQ to Entities
Write queries in C# with LINQ, which EF Core translates to SQL.
Example:
var products = context.Products.Where(p => p.Price > 50).ToList();
Change Tracking
Added
, Modified
, Deleted
) and updates DB on SaveChanges()
.Migrations
dotnet ef migrations add InitialCreate
dotnet ef database update
Database Seeding
OnModelCreating()
.Cross-Platform
Central class for EF Core.
Represents a session with the database.
Defines entity sets as DbSet<T>
.
Example:
public class AppDbContext : DbContext {
public DbSet<Product> Products { get; set; }
public DbSet<Order> Orders { get; set; }
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder) {
modelBuilder.Entity<Product>().HasKey(p => p.Id);
}
}