Entity Framework Core Migrations
EF Core Migrations is a feature that enables you to incrementally update your relational database schema to match your data model changes. When you make changes to your entity classes, such as adding a new property, removing a property, or changing a relationship, EF Core Migrations can help you translate these changes into database schema updates.
What are Migrations?
Migrations are like version control for your database schema. Each migration represents a specific set of changes to your database. EF Core keeps track of which migrations have been applied to your database, allowing you to automate the process of updating your database schema as your application evolves.
Key Concepts
- Data Model: The set of classes in your application that represent the data you're working with.
- Database Schema: The structure of your database, including tables, columns, relationships, etc.
- Migration: A file that contains code to change your database schema.
- Snapshot: A file that represents the current state of your database schema after the last migration was applied.
Getting Started with Migrations
To use EF Core Migrations, you'll typically use the .NET CLI or the Package Manager Console in Visual Studio.
1. Install the EF Core Tools
First, ensure you have the EF Core tools installed. For the .NET CLI:
dotnet tool install --global dotnet-ef
For Package Manager Console:
Install-Package Microsoft.EntityFrameworkCore.Tools
2. Enable Migrations
If you're starting a new project or haven't enabled migrations yet, you can do so with the following command:
dotnet ef migrations add InitialCreate
This command will generate the initial migration files, typically in a `Migrations` folder within your project. The `InitialCreate` is a descriptive name for your first migration.
3. Creating Subsequent Migrations
After making changes to your data model, you'll create new migrations to reflect those changes. For example, if you add a new property to an entity:
dotnet ef migrations add AddNewPropertyToUser
EF Core will compare your current data model with the last migration snapshot and generate the necessary C# code to update your database schema.
4. Applying Migrations to the Database
Once you've created a migration, you need to apply it to your database.
dotnet ef database update
This command will bring your database schema up to the latest migration version.
Example: Adding a New Property
Let's say you have an `Author` entity:
public class Author
{
public int Id { get; set; }
public string Name { get; set; }
}
And you decide to add an `Email` property:
public class Author
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
After running dotnet ef migrations add AddAuthorEmail
, EF Core will generate a migration file similar to this:
public partial class AddAuthorEmail : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Email",
table: "Authors",
type: "nvarchar(max)",
nullable: true);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Email",
table: "Authors");
}
}
The Up
method contains the code to apply the change (add the column), and the Down
method contains the code to revert the change (drop the column).
Common Migration Commands
dotnet ef migrations add <MigrationName>
: Creates a new migration.dotnet ef migrations list
: Lists all available migrations and their status.dotnet ef database update [<TargetMigration>]
: Applies migrations to the database. If no target is specified, it updates to the latest.dotnet ef migrations remove
: Removes the last migration.dotnet ef migrations script [<FromMigration>] [<ToMigration>]
: Generates a SQL script for migrations.
dotnet ef migrations script
and apply them manually or through a separate deployment process to have more control.
Working with Existing Databases
If you're starting with an existing database that EF Core doesn't manage, you can use the Scaffold-DbContext
command to generate your C# entity classes and a DbContext
from the existing database. You can then use Migrations to manage future schema changes.
Conclusion
EF Core Migrations is a powerful tool for managing your database schema in sync with your application's data model. By leveraging migrations, you can ensure consistency, automate deployments, and reduce the risk of manual database errors.