Migration in Entity Framework Core
In this tutorial we are going to se how to do migration in entity framework core.
One of the key features of Entity Framework is its ability to automatically migrate data model changes to the database schema. This process is called “database migration”.
How migration works
- You make changes to your data model (e.g. add a new entity, rename a property, etc.).
- You use the EF Package Manager Console (PMC) or the dotnet EF command-line tool to create a migration based on these changes.
- The EF generates code that represents the changes you made to the data model and stores it in a migration file.
- You apply the migration to the database using the EF PMC or the dotnet EF tool. This will update the database schema to match the data model.
There are two main approaches to database migration with Entity Framework:
Code First Migrations
This approach starts with the data model defined in code, and the EF is used to generate the database schema based on the data model. Code First Migrations are the recommended approach for new projects because they allow you to define your data model in a flexible and expressive way, and they make it easy to evolve the data model as the application grows.
Here’s how it works:
- You define your data model using classes and properties in code.
- You use the EF Package Manager Console (PMC) or the dotnet EF command-line tool to create a new database based on the data model.
- As you make changes to the data model, you create new migrations to represent the changes. The EF generates code that represents the changes and stores it in a migration file.
- You apply the migrations to the database using the EF PMC or the dotnet EF tool. This will update the database schema to match the data model.
Code First Migrations are the recommended approach for new projects because they allow you to define your data model in a flexible and expressive way, and they make it easy to evolve the data model as the application grows.
To use Code First Migrations, you need to enable it in your project by installing the Entity Framework NuGet package and adding a few lines of code to your application. Here’s a high-level overview of the steps involved:
Install the Entity Framework NuGet package:
Install-Package EntityFramework
Enable migrations in your project:
dotnet ef migrations add InitialCreate
Define your data model using classes and properties:
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
}
public class Course
{
public int Id { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
}
public class SchoolContext : DbContext
{
public DbSet<Student> Students { get; set; }
public DbSet<Course> Courses { get; set; }
}
Create a new database based on the data model:
dotnet ef database update
Make changes to the data model and create a new migration to represent the changes:
public class Student
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
dotnet ef migrations add RenameNameToFirstAndLastName
Apply the migration to the database:
dotnet ef database update
This is a high-level overview of how to use Code First Migrations with Entity Framework. There are many more features and options available, such as the ability to seed the database with initial data, to specify custom column types and constraints, and to customize the database schema using Fluent API.
In summary, Code First Migrations is a powerful and flexible feature of Entity Framework that enables you to manage and apply database migrations in your .NET projects. It allows you to define your data model in code, and it makes it easy to evolve the data model over time and keep the database schema in sync with the data model.
Database First Migrations
Database First Migrations is a feature of Entity Framework that enables you to generate a data model based on an existing database and apply changes to the data model to the database schema.
Here’s how it works:
- You start with an existing database that you want to use in your application.
- You use the EF Package Manager Console (PMC) or the dotnet EF command-line tool to generate a data model based on the database schema.
- As you make changes to the data model, you create new migrations to represent the changes. The EF generates code that represents the changes and stores it in a migration file.
- You apply the migrations to the database using the EF PMC or the dotnet EF tool. This will update the database schema to match the data model.
Database First Migrations are useful when you need to work with an existing database and you don’t have the option to redesign it. It can also be a good option if you prefer to design the database schema first and then generate the data model based on it.
To use Database First Migrations, you need to enable it in your project by installing the Entity Framework NuGet package and adding a few lines of code to your application. Here’s a high-level overview of the steps involved:
Install the Entity Framework NuGet package:
Install-Package EntityFramework
Connect to the existing database using the EF PMC or the dotnet EF tool:
dotnet ef database update
Generate a data model based on the database schema:
dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=SchoolDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Models
Make changes to the data model and create a new migration to represent the changes:
public class Student
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
dotnet ef migrations add RenameNameToFirstAndLastName
Apply the migration to the database:
dotnet ef database update
This is a high-level overview of how to use Database First Migrations with Entity Framework. There are many more features and options available, such as the ability to seed the database with initial data, to specify custom column types and constraints, and to customize the database schema using Fluent API.
In summary, Database First Migrations is a feature of Entity Framework that enables you to work with an existing database and apply changes to the data model to the database schema. It allows you to generate a data model based on the database schema, and it makes it easy to evolve the data model over time and keep the database schema in sync with the data model.