Русские видео

Сейчас в тренде

Иностранные видео




Если кнопки скачивания не загрузились НАЖМИТЕ ЗДЕСЬ или обновите страницу
Если возникают проблемы со скачиванием, пожалуйста напишите в поддержку по адресу внизу страницы.
Спасибо за использование сервиса savevideohd.ru



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.    / @aarvikitchen5572   Text version of the video https://csharp-video-tutorials.blogsp... Slides https://csharp-video-tutorials.blogsp... ASP.NET Core Text Articles & Slides https://csharp-video-tutorials.blogsp... ASP.NET Core Tutorial    • ASP.NET core tutorial for beginners   Angular, JavaScript, jQuery, Dot Net & SQL Playlists https://www.youtube.com/user/kudvenka... 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 = "[email protected]" } ); } } 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 = "[email protected]" }, new Employee { Id = 2, Name = "John", Department = Dept.HR, Email = "[email protected]" } ); } 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 = "[email protected]" }, new Employee { Id = 2, Name = "John", Department = Dept.HR, Email = "[email protected]" } ); } } } In OnModelCreating() method of the DbContext class, you can then call Seed() method as shown below. protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Seed(); }

Comments