The Commands and APIs You Reach For Daily
This reference collects the EF Core CLI commands, common LINQ query patterns, and Fluent API configuration snippets that come up constantly in day-to-day development, so you don't have to hunt through full documentation for syntax you've seen before but don't have memorized. It assumes you already understand the underlying concepts covered elsewhere in this course — this page is deliberately terse and example-driven rather than explanatory, organized so you can scan for the shape of the syntax you need.
Cricket analogy: A quick reference card is like a fielding captain's laminated cheat sheet of field placements for different bowlers, glanced at between overs rather than re-deriving strategy from scratch each time, the same fast-recall purpose this page serves for EF Core syntax.
CLI Commands
The dotnet ef tool (installed via dotnet tool install --global dotnet-ef) drives migrations and scaffolding from the command line. The core migration workflow is add, then update or script, with remove available to undo an unapplied migration before it's committed. Scaffolding an existing database into entity classes uses dbcontext scaffold, and dbcontext info/list are useful for confirming which context and connection string a project resolves at design time.
Cricket analogy: The dotnet ef workflow of add, then update, mirrors a captain calling for a review with the third umpire and then acting on the ruling — you propose a change, then apply it once it's confirmed correct.
# Create a new migration after model changes
dotnet ef migrations add AddCustomerLoyaltyPoints
# Apply pending migrations directly to the database
dotnet ef database update
# Roll back to a specific earlier migration
dotnet ef database update PreviousMigrationName
# Generate an idempotent SQL script for a controlled deployment
dotnet ef migrations script --idempotent -o deploy.sql
# Remove the last unapplied migration
dotnet ef migrations remove
# Scaffold entity classes from an existing database
dotnet ef dbcontext scaffold "Server=.;Database=Shop;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o ModelsCommon LINQ and Fluent API Patterns
The most reached-for LINQ patterns are filtering with Where(), shaping results with Select(), paging with OrderBy().Skip().Take(), and aggregation with GroupBy() or the async terminal operators (ToListAsync, FirstOrDefaultAsync, CountAsync, AnyAsync). On the configuration side, Fluent API calls inside OnModelCreating — HasKey, HasIndex, HasOne().WithMany(), Property().HasMaxLength(), and IsRequired() — are the ones used in nearly every model, while data annotations like [Required], [MaxLength], and [Key] cover the same ground more tersely for simple cases directly on entity classes.
Cricket analogy: Where() is like a selector filtering the squad down to players available for an away tour, and OrderBy().Take() is like picking a fixed batting order of eleven from that filtered pool, the same narrowing-then-shaping LINQ performs on a query.
// LINQ patterns
var recentOrders = await context.Orders
.Where(o => o.Status == OrderStatus.Shipped)
.OrderByDescending(o => o.PlacedAt)
.Skip(20).Take(10)
.Select(o => new { o.Id, o.Total })
.ToListAsync();
var hasPending = await context.Orders.AnyAsync(o => o.Status == OrderStatus.Pending);
var countsByStatus = await context.Orders
.GroupBy(o => o.Status)
.Select(g => new { Status = g.Key, Count = g.Count() })
.ToListAsync();
// Fluent API configuration
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Order>(e =>
{
e.HasKey(o => o.Id);
e.Property(o => o.Status).HasConversion<string>().HasMaxLength(20);
e.HasIndex(o => o.CustomerId);
e.HasOne(o => o.Customer).WithMany(c => c.Orders).HasForeignKey(o => o.CustomerId);
});
}Terminal async operators like ToListAsync(), FirstOrDefaultAsync(), CountAsync(), and AnyAsync() execute the query against the database — everything before them just builds an expression tree. Forgetting the Async suffix (using .First() instead of .FirstOrDefaultAsync()) still works but blocks a thread synchronously instead of freeing it during I/O.
dotnet ef database update with no target migration applies every pending migration. If you actually meant to roll back, pass the name of the migration you want to roll back TO — passing the name of the migration you want to undo rolls back past it, not to it.
- dotnet ef migrations add/remove manage migration files; database update/script apply them.
- Use --idempotent SQL scripts for controlled production deployments instead of automatic Database.Migrate().
- Where(), Select(), OrderBy().Skip().Take(), and GroupBy() cover the vast majority of everyday LINQ needs.
- Async terminal operators (ToListAsync, FirstOrDefaultAsync, AnyAsync, CountAsync) are what actually execute a query.
- Fluent API in OnModelCreating (HasKey, HasIndex, HasOne/WithMany, HasMaxLength, IsRequired) configures the model precisely.
- Data annotations like [Required] and [MaxLength] cover simple cases directly on entity classes.
Practice what you learned
1. Which command generates a new EF Core migration file after model changes?
2. What does dotnet ef migrations script --idempotent produce?
3. Which LINQ operator actually triggers query execution against the database?
4. In Fluent API, what does HasOne().WithMany() configure?
5. What does dotnet ef dbcontext scaffold do?
Was this page helpful?
You May Also Like
EF Core Best Practices
A practical checklist of habits that keep EF Core applications fast, predictable, and maintainable as they scale.
EF Core vs Dapper
A comparison of EF Core's full ORM feature set against Dapper's lightweight micro-ORM approach, and how to choose or combine them.
EF Core Interview Questions
Common EF Core interview topics — change tracking, migrations, loading strategies, and concurrency — explained with the reasoning behind strong answers.
Related Reading
Related Study Notes in Microsoft Technologies
Browse all study notesWindows 10 / UWP Development Study Notes
.NET · 30 topics
Microsoft TechnologiesWindows Batch Scripting Study Notes
Batch · 30 topics
Microsoft TechnologiesMFC (Microsoft Foundation Classes) Study Notes
C++ · 30 topics
Microsoft TechnologiesSilverlight Study Notes
.NET · 30 topics
Microsoft TechnologiesXAML Study Notes
.NET · 30 topics
Microsoft TechnologiesWPF (Windows Presentation Foundation) Study Notes
.NET · 30 topics