Suggested Videos
Part 48 - Using sql server with entity framework core | Text | Slides
Part 49 - Repository pattern in asp.net core | Text | Slides
Part 50 - Entity framework core migrations | Text | Slides
In this video we will discuss how to seed database tables with initial data using Migrations in entity framework core.
If you are using Entity Framework Core 2.1 or later there is a new method of seeding database data. In your application DbContext class, override OnModelCreating() method. In this example, HasData() method configures Employee entity to have the specified seed data.
Using Migrations to seed data
The following command adds a new migration. I named the migration SeedEmployeesTable as we are using this migration to specifically add seed data to the Employees database table.
The above command generates the following code
Finally, execute Update-Database command to apply the above migration to the database.
Altering Existing Database Seed data
You can alter the existing seed data or add new seed data by adding another new migration.
Step 1 : Modify the code in OnModelCreating() method.
Step 2 : Add a new migration
Step 3 : Update the database with the latest migration
Keeping DbContext Class Clean
To keep the DbContext class clean, you may move the seeding code from the DbContext class into an extension method on the ModelBuilder class.
In OnModelCreating() method of the DbContext class, you can then call Seed() method as shown below.
Part 48 - Using sql server with entity framework core | Text | Slides
Part 49 - Repository pattern in asp.net core | Text | Slides
Part 50 - Entity framework core migrations | Text | Slides
In this video we will discuss how to seed database tables with initial data using Migrations in entity framework core.
If you are using Entity Framework Core 2.1 or later there is a new method of seeding database data. In your application DbContext class, override OnModelCreating() method. In this example, HasData() method configures Employee entity to have the specified seed data.
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Employee> Employees { get; set; }
protected override void
OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>().HasData(
new Employee
{
Id = 1,
Name = "Mark",
Department = Dept.IT,
Email = "mark@pragimtech.com"
}
);
}
}
Using Migrations to seed data
The following command adds a new migration. I named the migration SeedEmployeesTable as we are using this migration to specifically add seed data to the Employees database table.
Add-Migration SeedEmployeesTable
The above command generates the following code
using Microsoft.EntityFrameworkCore.Migrations;
namespace EmployeeManagement.Migrations
{
public partial class SeedEmployeesTable : Migration
{
protected override void
Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.InsertData(
table: "Employees",
columns: new[] { "Id", "Department", "Email", "Name" },
values: new object[] { 1, 3, "mark@pragimtech.com", "Mark" });
}
protected override void
Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DeleteData(
table: "Employees",
keyColumn: "Id",
keyValue: 1);
}
}
}
Finally, execute Update-Database command to apply the above migration to the database.
Altering Existing Database Seed data
You can alter the existing seed data or add new seed data by adding another new migration.
Step 1 : Modify the code in OnModelCreating() method.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>().HasData(
new Employee
{
Id = 1,
Name = "Mary",
Department = Dept.IT,
Email = "mary@pragimtech.com"
},
new Employee
{
Id = 2,
Name = "John",
Department = Dept.HR,
Email = "john@pragimtech.com"
}
);
}
Step 2 : Add a new migration
Add-Migration AlterEmployeesSeedData
Step 3 : Update the database with the latest migration
Update-Database
Keeping DbContext Class Clean
To keep the DbContext class clean, you may move the seeding code from the DbContext class into an extension method on the ModelBuilder class.
using Microsoft.EntityFrameworkCore;
namespace EmployeeManagement.Models
{
public static class ModelBuilderExtensions
{
public static void Seed(this ModelBuilder
modelBuilder)
{
modelBuilder.Entity<Employee>().HasData(
new Employee
{
Id = 1,
Name = "Mary",
Department = Dept.IT,
Email = "mary@pragimtech.com"
},
new Employee
{
Id = 2,
Name = "John",
Department = Dept.HR,
Email = "john@pragimtech.com"
}
);
}
}
}
In OnModelCreating() method of the DbContext class, you can then call Seed() method as shown below.
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Seed();
}
{
modelBuilder.Seed();
}
how to seed data to only specific environment.
ReplyDelete