In this post we will show how to add a seed data to entity framework in asp.net boilerplate. I was in a impression that Entity Framework Core will add the seed data on the migration file and it will create it when we run update-database. But I was wrong, actually Entity Framework Core does not support Seeding database on running migrations.
Thus what is actually done, is when you run the application at that time the seed data is added into the database. Let's go through how to create a file and add a Seed Data to it in ASP.NET Boilerplate:
At first we will create a class inside the LetsDisc.Core
Then in LetsDisc.EntityFrameworkCore, we will add another class which will add the Seed data to this Table. Do note that since we add the data on the application load we do have to check if the data exists, if the data do not exists then only add the data.
Also since we have added a new class, we have to add that class in LetsDiscDbContext as shown below :
Thus after that, I will do the below steps to get the table in my database:
1. First delete the database
2. Run `Remove-Migration` from Package Manager Console
3. Run `Add-Migration LetsDisc-v1` to add the latest migration
4. Run `update-database` to update the database
When I will run the application now, I will get the initial data into the database
Checkout the Full Code at: https://github.com/codingdefined/LetsDisc/commit/237ef81d780c7d8fdf59e3f0db421c9b98442834
Thus what is actually done, is when you run the application at that time the seed data is added into the database. Let's go through how to create a file and add a Seed Data to it in ASP.NET Boilerplate:
At first we will create a class inside the LetsDisc.Core
using System.ComponentModel.DataAnnotations; namespace LetsDisc.Posts { public class PostTypes { [Key] public string Name { get; set; } public PostTypes(string name) { Name = name; } } }
Then in LetsDisc.EntityFrameworkCore, we will add another class which will add the Seed data to this Table. Do note that since we add the data on the application load we do have to check if the data exists, if the data do not exists then only add the data.
using System.Collections.Generic; using LetsDisc.Posts; using System.Linq; namespace LetsDisc.EntityFrameworkCore.Seed.Host { class DefaultPostTypesCreator { public static List<PostTypes> InitialPostTypes => GetInitialPostType(); private readonly LetsDiscDbContext _context; private static List<PostTypes> GetInitialPostType() { return new List<PostTypes> { new PostTypes("Question"), new PostTypes("Answer"), new PostTypes("Tag Wiki") }; } public DefaultPostTypesCreator(LetsDiscDbContext context) { _context = context; } public void Create() { CreatePostTypes(); } private void CreatePostTypes() { foreach (var postType in InitialPostTypes) { AddPostTypeIfNotExists(postType); } } private void AddPostTypeIfNotExists(PostTypes postType) { if(_context.PostTypes.Any(p => p.Name == postType.Name)) { return; } _context.PostTypes.Add(postType); _context.SaveChanges(); } } }Once created we need to call the DefaultPostTypesCreator's Create() function from InitialHostDbBuilder Class as shwobn
namespace LetsDisc.EntityFrameworkCore.Seed.Host { public class InitialHostDbBuilder { public void Create() { //Initial Code ... new DefaultPostTypesCreator(_context).Create(); _context.SaveChanges(); } } }
Also since we have added a new class, we have to add that class in LetsDiscDbContext as shown below :
public virtual DbSet<PostTypes> PostTypes { get; set; }
Thus after that, I will do the below steps to get the table in my database:
1. First delete the database
2. Run `Remove-Migration` from Package Manager Console
3. Run `Add-Migration LetsDisc-v1` to add the latest migration
4. Run `update-database` to update the database
When I will run the application now, I will get the initial data into the database
Checkout the Full Code at: https://github.com/codingdefined/LetsDisc/commit/237ef81d780c7d8fdf59e3f0db421c9b98442834
No comments:
Post a Comment