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(); } } }
The code is pretty straight forward it checks for categories in the database with the line
context.Categories.Any()
if there are Categories in the database then it just get out of the method else it populates the database with the categories object
2. Now open the Startup.cs file and add the NorthwindContext to the Configure method
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, NorthwindContext context) { app.UseStaticFiles(); app.UseMvc(config => { config.MapRoute( name: "Default", template: "{controller}/{action}/{id?}", defaults: new { controller = "Home", action = "Index" } ); }); DbInitializer.Initialize(context); }
Add the line DbIntitializer.Initialize(context) to the end of the method to seed the database on startup Run the application and the Categories table will be populated with seed data
The Products table will be populated as well notice how the CategoryId has been assigned to the appropriate category
ASP.NET Core Posts:
- How To Create An ASP.NET Core Application From Scratch
- ASP.NET Core : Add jQuery, Bootstrap, AngularJS Using bower.json
- Enable MVC On ASP.NET Core Application
- ASP.NET Core : Create Our First Controller and View
- ASP.NET Core : Adding The Default View With _ViewStart.cshtml
- ASP.NET Core : Create A Responsive Layout With Bootstrap
- ASP.NET Core : Adding Font-Awesome For Northwind Cafe Navigation Icons
- ASP.NET Core : Add .json Configuration Files With Microsoft.Extensions.Configuration Library
- ASP.NET Core : Entity Framework Core Models For Northwind Cafe
- ASP.NET Core : Create The NothwindContext ( EntityFrameworkCore )
- ASP.NET Core : Configure project.json File To Support Entity Framework Core
- ASP.NET Core : Add NorthwindContext To Startup Class And Create Database
- ASP.NET Core: Seeding The NorwindCafe Database
- ASP.NET Core: Add Logging To The NorthwindCafe Application
context.Categories.Any() is not working on my project.
ReplyDeleteWhat's happening is, it inserts the data everytime my application execute. Im using VSCode EntityFrameworkCore in Ubuntu Linux
Does your Categories object have an Id? I suspect it is not executing a check on the database with just .Any() alone.
ReplyDeleteYou can try the following: if (context.Categories.Any(x => x.Id > 0)) { }
While this article focuses on the use of databases for marketing leisure travel, the principles of database organization may be applied to any database. create dashboard for oracle
ReplyDeleteAivivu đại lý vé máy bay, tham khảo
ReplyDeleteVe may bay di My
vé máy bay từ mỹ về việt nam 2021
đã có chuyến bay từ nhật về việt nam chưa
mua vé máy bay từ đức về việt nam
giá vé máy bay từ Vancouver về việt nam
gia ve may bay vietjet tu han quoc ve viet nam
vé máy bay cho chuyên gia nước ngoài
This approach seeds the database using EF Core's built-in functionality, making it easy cookout chicken wrap to manage initial data for your application.
ReplyDelete"Great guide on seeding databases in ASP.NET Core! Adding `DbInitializer.Initialize(context)` in `Startup.cs` is an effective way to prepopulate data for development. Just a tip: for production, migrations are safer to manage data updates.
ReplyDeleteWhile building my site on restaurant menus, like Cookout’s offerings, I used similar techniques to test features. Check it out here: Cookout Menu Excited for more ASP.NET Core insights!"
Great guide on seeding the NorthwindCafe database in ASP.NET Core! Database seeding is crucial for initializing apps with test data efficiently. For those who love exploring structured data, check out "https://cookoutsmenu.us" for a well-organized Cookout menu breakdown!
ReplyDeletehttps://cookoutsmenu.us
ReplyDeleteAmazing, Your blogs are really good and informative. I got a lot of useful information in your blogs. mcdo breakfast
ReplyDeleteAmazing! Your blogs are really informative and well-researched. I found a lot of useful insights here. Looking forward to more great content. Also, check out this delicious Filipino Pork BBQ for an authentic taste visit here. https://amber-menu.com.ph
ReplyDeleteIn ASP.NET Core, seeding the NorthwindCafe database involves adding initial data to tables at application startup. This can be done by creating a seed method in the Startup.cs or Program.cs file. Use context.SaveChanges() to persist data, often within Configure or ConfigureServices methods after migrations. Visit: Khaleej Day Website
ReplyDeleteResidential and Commercial Roofing Contractor in Cheyenne, Wyoming commercial roofing contractors: Trusted roofing services for homes and businesses in Cheyenne and surrounding areas. Specializing in high-quality roof installations, repairs, and maintenance. Ensure the longevity and durability of your roof with our expert team.
ReplyDeleteIf you're a food lover and want to enjoy delicious meals during your busy schedule, Cheddar’s gives you the perfect relaxing time.
ReplyDeleteVisit Cheddar's Menu