Cheatsheet for Entity Framework Core
Steve Mak
Posted on May 29, 2020
EF Core Command from Package Manager Console (PMC) / .NET CLI
Reference
- https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=vs
- https://docs.microsoft.com/en-us/ef/core/modeling/entity-properties?tabs=data-annotations%2Cwithout-nrt
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
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
💖 💪 🙅 🚩
Steve Mak
Posted on May 29, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.