In the previous blog we created the NorthwindCafe database with Entity Framework Core. Now we are going to seed the database so that we can work with the data.
Here are the steps to seed the NorthwindCafe database:
1. Create a file call DBInitializer in the NorthwindCafe.Web folder, in the file type in the following code
using System.Linq;
namespace NorthwindCafe.Web.Models
{
public class DbInitializer
{
public static void Initialize(NorthwindContext context)
{
context.Database.EnsureCreated();
if(context.Categories.Any())
{
return;
}
var categories = new Category[]
{
new Category {Name = "Coffee", Description="Coffee", Products = new Product[] { new Product { Name = "Dark Roast", Description = "Dark Roast", Price = 2.0M } } },
new Category {Name = "Tea", Description="Tea", Products = new Product[] { new Product { Name = "Chai", Description = "Chai", Price = 1.5M } } },
new Category {Name = "Pastry", Description="Pastry", Products = new Product[] { new Product { Name = "Cupcake", Description = "Cupcake", Price = 1.25M } } },
new Category {Name = "Food", Description = "Food", Products = new Product[] { new Product { Name = "Hamburger", Description = "Hamburger", Price = 5.0M } } }
};
foreach (var c in categories)
{
context.Categories.Add(c);
}
context.SaveChanges();
}
}
}