Lesson 6 of 40
Data Access
Intermediate
55 min
Entity Framework Core 10
Master EF Core 10's new bulk operations, JSON column support, complex type improvements, and the overhauled migration system.
Part 1: Bulk Operations (ExecuteUpdate/Delete)
// Update without loading entities into memory
await context.Orders
.Where(o => o.Status == "Pending" && o.CreatedAt < cutoff)
.ExecuteUpdateAsync(s => s
.SetProperty(o => o.Status, "Expired")
.SetProperty(o => o.UpdatedAt, DateTime.UtcNow));
await context.Orders
.Where(o => o.Status == "Pending" && o.CreatedAt < cutoff)
.ExecuteUpdateAsync(s => s
.SetProperty(o => o.Status, "Expired")
.SetProperty(o => o.UpdatedAt, DateTime.UtcNow));
Part 2: JSON Columns
Store complex objects as JSON in relational databases with full LINQ querying:
public class Order
{
public int Id { get; set; }
public ShippingAddress Address { get; set; } // Stored as JSON
}
// LINQ query into JSON column:
context.Orders.Where(o => o.Address.Country == "US")
{
public int Id { get; set; }
public ShippingAddress Address { get; set; } // Stored as JSON
}
// LINQ query into JSON column:
context.Orders.Where(o => o.Address.Country == "US")
Part 3: Complex Types (Value Objects)
EF Core 10 supports complex types — value objects without identity:
// Configuration
modelBuilder.Entity<Order>()
.ComplexProperty(o => o.Money, b => {
b.Property(m => m.Amount).HasPrecision(18, 4);
b.Property(m => m.Currency).HasMaxLength(3);
});
modelBuilder.Entity<Order>()
.ComplexProperty(o => o.Money, b => {
b.Property(m => m.Amount).HasPrecision(18, 4);
b.Property(m => m.Currency).HasMaxLength(3);
});
Part 4: Migrations Best Practices
| Practice | Command |
|---|---|
| Add migration | dotnet ef migrations add MigrationName |
| Generate SQL script | dotnet ef migrations script |
| Apply to production | dotnet ef database update --connection "..." |
| Bundle for CI/CD | dotnet ef migrations bundle |