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
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
Показать
Комментарии отсутствуют
Информация о видео
Другие видео канала
![Keeping domain models and database schema in sync in asp net core](https://i.ytimg.com/vi/MhvOKHUWgiY/default.jpg)
![CRUD with a .NET 6 Web API & Entity Framework Core 🚀 Full Course](https://i.ytimg.com/vi/Fbf_ua2t6v4/default.jpg)
![](https://i.ytimg.com/vi/Iae0pfVrZ1o/default.jpg)
![Migrations and Seed Data in Entity Framework Core](https://i.ytimg.com/vi/5r_p8TiNX3Y/default.jpg)
![11. Seeding Your Database with Data](https://i.ytimg.com/vi/7DqT__ZOtG4/default.jpg)
![Entity Framework Core Part 11 - Relationships](https://i.ytimg.com/vi/eHT6G912po0/default.jpg)
![Repository pattern in asp net core](https://i.ytimg.com/vi/qJmEI2LtXIY/default.jpg)
![Part 3 - Seeding Data with Entity Framework for ASP.NET Core 6.0 API](https://i.ytimg.com/vi/w9ebezbnb9s/default.jpg)
![Extend IdentityUser in ASP NET Core](https://i.ytimg.com/vi/NV734cJdZts/default.jpg)
![Entity Framework Core Migrations with ASP.NET Core in C#](https://i.ytimg.com/vi/bZ74dirFHUA/default.jpg)
![.NET 6 Web API Authentication | Minimal API & Swagger (CRUD)](https://i.ytimg.com/vi/f2IdQqpjR0c/default.jpg)
![Logging in ASP NET Core](https://i.ytimg.com/vi/WyAJe6lA-bY/default.jpg)
![Logging into Elasticsearch using Serilog and viewing logs in Kibana | .NET Core Tutorial](https://i.ytimg.com/vi/0acSdHJfk64/default.jpg)
![Entity framework core migrations](https://i.ytimg.com/vi/G14lmWS-h4k/default.jpg)
![Claims based authorization in asp net core](https://i.ytimg.com/vi/LJQBBvJ6tL0/default.jpg)
![Authorization in ASP NET Core](https://i.ytimg.com/vi/uET7MjhUeY4/default.jpg)
![Logging to file in asp net core using nlog](https://i.ytimg.com/vi/o5u4fE0t79k/default.jpg)
![Global exception handling in asp net core mvc](https://i.ytimg.com/vi/jeBttUIqpuc/default.jpg)
![16. Seeding your database | ASP.NET MVC](https://i.ytimg.com/vi/_6uKjv-BLNc/default.jpg)
![ASP NET Core LogLevel configuration](https://i.ytimg.com/vi/bTPnT13Efd4/default.jpg)