Database Migrations
Learn how database migrations work in Entity Framework Core and why they are essential for creating and updating your database schema in a controlled, organized way.
Part 1: What Is a Migration?
A migration is a way to describe changes to your database structure over time. Instead of manually creating tables and columns in SQL Server, Entity Framework Core can generate these changes for you based on your model classes.
This means that if you change your model, EF Core can help keep the database in sync.
Part 2: Why Migrations Matter
As your application grows, your data model will change. You may add new properties, remove unused ones, or create new entities entirely.
Migrations are important because they:
- Track database structure changes over time
- Keep the database aligned with your models
- Reduce the need for manual SQL scripts
- Make development and deployment more organized
Part 3: A Simple Example
Suppose your Student model originally looks like this:
{
public int Id { get; set; }
public string Name { get; set; }
public string Course { get; set; }
}
Later, you decide to add an Email property:
{
public int Id { get; set; }
public string Name { get; set; }
public string Course { get; set; }
public string Email { get; set; }
}
EF Core can detect this change and create a migration to add the new Email column to the database.
Part 4: Creating a Migration
To create a migration, you normally use the Package Manager Console or command line tools. In Visual Studio, a common command is:
This creates a migration file that represents the database structure for your current models.
If you later make more changes, you create another migration with a different name, such as:
Part 5: Applying a Migration
After creating a migration, you apply it to the database using:
This tells EF Core to execute the migration and update the database schema.
If the database does not exist yet, EF Core can create it. If it already exists, EF Core applies only the necessary changes.
Part 6: What EF Core Generates
When you create a migration, EF Core generates files that contain instructions for the database changes.
These files are usually stored in a Migrations folder.
Each migration describes:
- What should be added, changed, or removed
- How to apply the change
- How to reverse the change if needed
This gives your database schema a history, similar to version control for code.
Part 7: Migrations in the Student Project
In your Student CRUD application, migrations make it easy to evolve the database as the project grows. For example:
- Add Email to Student
- Add PhoneNumber later
- Create a new Course entity
- Modify validation-related column lengths
Instead of rebuilding the whole database manually, migrations apply only the required updates.
Part 8: Best Practices
- Give migrations clear and meaningful names
- Create a migration whenever your model changes
- Review generated migrations before applying them
- Keep migration files under source control
- Do not edit the database structure manually unless necessary
These habits make your project easier to maintain, especially as it becomes larger or more collaborative.
Summary
Database migrations are one of the most useful features of Entity Framework Core. They help your ASP.NET Core application evolve safely by keeping the database structure synchronized with your model classes. Once you understand migrations, managing schema changes becomes far more organized and reliable.