Загрузка страницы

Entity framework core seed data

In this video we will discuss how to seed database tables with initial data using Migrations in entity framework core.

Healthy diet is very important for both body and mind. We want to inspire you to cook and eat healthy. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation=1

Text version of the video
https://csharp-video-tutorials.blogspot.com/2019/05/entity-framework-core-seed-data.html

Slides
https://csharp-video-tutorials.blogspot.com/2019/05/entity-framework-core-seed-data-slides.html

ASP.NET Core Text Articles & Slides
https://csharp-video-tutorials.blogspot.com/2019/01/aspnet-core-tutorial-for-beginners.html

ASP.NET Core Tutorial
https://www.youtube.com/playlist?list=PL6n9fhu94yhVkdrusLaQsfERmL_Jh4XmU

Angular, JavaScript, jQuery, Dot Net & SQL Playlists
https://www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd
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

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();
}

Видео Entity framework core seed data канала kudvenkat
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
2 мая 2019 г. 0:04:25
00:07:20
Яндекс.Метрика