DEV Community

Steve Mak
Steve Mak

Posted on • Edited on

Cheatsheet for Entity Framework Core

EF Core Command from Package Manager Console (PMC) / .NET CLI

Reference

PMC .NET CLI Usage
Add-Migration Add Creates a migration by adding a migration snapshot.
Remove-migration Remove Removes the last migration snapshot.
Update-database Update Updates the database schema based on the last migration snapshot.
Script-migration Script Generates a SQL script using all the migration snapshots.

Code First

# PMC
PM > Add-Migration InitialCreate
PM > Update-Database
PM > Remove-Migration
PM > Script-Migration

# .NET CLI
> dotnet ef migrations add InitialCreate
> dotnet ef database update
> dotnet ef migrations remove
> dotnet ef migrations script
Enter fullscreen mode Exit fullscreen mode

Database First

# PMC
PM > Scaffold-DbContext 'Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook' Microsoft.EntityFrameworkCore.SqlServer

# .NET CLI
> dotnet ef dbcontext scaffold "Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Chinook" Microsoft.EntityFrameworkCore.SqlServer
Enter fullscreen mode Exit fullscreen mode

Top comments (0)