Migrations and Seeding in ASP.NET Core and Entity Framework Core
ASP.NET Core and Entity Framework Core provide the capability of creating migrations and seeding data to the database. Migrations enable developers to evolve the database schema over time, while seeding allows developers to populate an empty database with sample data. In this page, we will explore how migrations and seeding work in ASP.NET Core and Entity Framework Core.
Syntax
To create migrations, you can use Entity Framework Core commands. Here's an example of creating a migration for the Blog
entity:
dotnet ef migrations add AddBlogEntity
To apply migrations to the database, you can use the update
command:
dotnet ef database update
To seed data to the database, you can override the OnModelCreating
method of DbContext
and add your seeding code.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>().HasData(
new Blog
{
Id = 1,
Title = "First Blog",
Content = "This is the first blog"
},
new Blog
{
Id = 2,
Title = "Second Blog",
Content = "This is the second blog"
});
}
Example
Let's assume that we have an empty database and we want to create a migration to add a Blog
entity. Here is the code for the Blog
entity:
public class Blog
{
public int Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
}
To create the migration, we use the Add-Migration
command in the Package Manager Console:
Add-Migration AddBlogEntity
This generates a new migration file with the name AddBlogEntity
. To apply the migration to the database, we use the Update-Database
command:
Update-Database
This creates the Blog
table in the database.
To seed data to the database, we override the OnModelCreating
method of DbContext
:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>().HasData(
new Blog
{
Id = 1,
Title = "First Blog",
Content = "This is the first blog"
},
new Blog
{
Id = 2,
Title = "Second Blog",
Content = "This is the second blog"
});
}
Calling the Update-Database
command now seeds the two Blog
entities to the database.
Output
When you create and apply migrations, you can view the changes in the database via database management tools. When you seed data, you can verify that the data has been properly seeded by querying the database.
Explanation
Migrations and seeding provide a way to update and initialize the database schema and data. Migrations enable developers to evolve the database schema over time, while seeding allows developers to populate an empty database with sample data. By using these two features together, developers can create and maintain a database with ease.
Use
Migrations and seeding are very useful when you need to evolve the database schema over time, or seed data into a new database. By using migrations and seeding, you have the freedom to update the database schema and data without manually making changes in the database.
Important Points
- Migrations enable developers to evolve the database schema over time.
- Seeding allows developers to populate an empty database with sample data.
- Use migrations and seeding to create and maintain a database with ease.
Summary
In this page, we discussed how to use migrations and seeding in ASP.NET Core and Entity Framework Core. We looked at the syntax, example, output, explanation, use, and important points of migrations and seeding. By using these two features together, developers can easily maintain and evolve the database schema and data.