EF Core Migrations
EF Core
Migrations:
Entity Framework
(EF) Core migrations help manage database schema changes over time. Migrations
allow evolving the database schema as the data model changes (e.g., adding or
removing properties or entities) without losing data. Each migration consists
of two methods: Up() for applying changes and Down() for
rolling back changes.
It is important
to back up the database before applying migrations in production. The dotnet
ef migrations script command can be used to generate and review SQL
scripts before applying them.
Migrations also
support seeding data and running custom SQL during the migration process. They
are essential for maintaining database consistency in evolving applications,
enabling easier collaboration in teams and ensuring the database schema stays
synchronized with the code.
Migration
Steps:
- Make changes to the model (e.g.,
add a new property).
- Run dotnet ef migrations add
<MigrationName> to generate a migration.
- Apply the migration by
running dotnet ef database update to update the database schema.
Commands:
· Creates
a migration after changes to the model.
o
dotnet ef migrations add <MigrationName>
· Applies
pending migrations to the database.
o
dotnet ef database update
· Removes
the last migration (before applying it to the database).
o
dotnet ef migrations remove
· Lists all
migrations.
o
dotnet ef migrations list
Comments
Post a Comment