Skip to main content

The command-line interface (CLI) tools for Entity Framework Core perform design-time development tasks.

##
# dotnet-ef commands
# Dotnet Core Entity Framework CLI commands and example usage.
#
# References
#
#   - Entity Framework Core tools reference - .NET CLI <https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet>
#   - Entity Framework Core Migrations <https://docs.microsoft.com/ef/core/managing-schemas/migrations/>
#   - Tutorial: Using the migrations feature - ASP.NET MVC with EF Core <https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations>
#   - Migrations and Seed Data with Entity Framework Core <https://code-maze.com/migrations-and-seed-data-efcore/>
#   - Getting Started with Entity Framework Core in ASP.NET Core - Models, DbContext, Configuration <https://code-maze.com/getting-started-with-efcore/>
#   - Entity Framework Core Relationships - Convention, Data Annotations and Fluent API <https://code-maze.com/efcore-relationships/>
#   - Database Queries in Entity Framework Core - <https://code-maze.com/queries-in-entity-framework-core/>
#   - ASP.NET Core Web API - Repository Pattern <https://code-maze.com/net-core-web-development-part4/>
#   - Use EFCore Migrations with multi-assembly project <https://github.com/bricelam/Sample-SplitMigrations>
##

# To install the dotnet core Entity Framework CLI globally (3.x):
dotnet tool install --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 run the database migration and update the database:
dotnet ef database update

# 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 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>