The command-line interface (CLI) tools for Entity Framework Core perform design-time development tasks.
# To install the tool globally:
dotnet tool install --global dotnet-ef
# To update the tool globally:
dotnet tool update --global dotnet-ef
# To create an initial migration:
dotnet ef migrations add InitialCreate
# To create a script for the InitialCreate migration:
dotnet ef migrations script 0 InitialCreate
# To create a script for all migrations after the InitialCreate migration:
dotnet ef migrations script <20180904195021_InitialCreate>
# To list available migrations:
dotnet ef migrations list
# To remove the last migration (roll back the code changes that were done for the migration):
dotnet ef migrations remove
# To generate a SQL script from migrations:
dotnet ef migrations script --output <migrations.sql>
# To generate a SQL script that can be used on a database at any migration (--idempotent):
dotnet ef migrations script --output <migrations.sql> --idempotent
# To apply all pending migrations:
dotnet ef database update
# To apply a specific migration:
dotnet ef database update MigrationName
# To revert to previous migration:
dotnet ef database update PreviousMigrationName
# To remove all migrations:
dotnet ef database update 0
# To drop the existing database:
dotnet ef database drop
# To get information about a DbContext type.
dotnet ef dbcontext info
# To list available DbContext types:
dotnet ef dbcontext list
# To test the database connection:
dotnet ef database verify
# To scaffold all schema's and tables and puts the new files in the Models folder:
dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models
# To scaffold only selected tables and creates the context in a separate folder with a specified name:
dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models -t Blog -t Post --context-dir Context -c BlogContext
# To generate a SQL script from migrations:
dotnet ef dbcontext script --context <dbcontext> --output <migrations.sql>
# To reset migration state:
dotnet ef database update 0
dotnet ef migrations remove
dotnet ef migrations add InitialCreate
# To force new migration:
dotnet ef migrations add MigrationName --force
# To generate a SQL script between specific migrations:
dotnet ef migrations script FromMigration ToMigration
# To generate a SQL script for all migrations:
dotnet ef migrations script
# To generate an idempotent SQL script (safe to run multiple times):
dotnet ef migrations script --idempotent