Friday, August 23, 2019
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 "Models" folder, in the file type in the following code
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
Add the line DbIntitializer.Initialize(context) to the end of the method to seed the database on startup Run by pressing CTRL+F5 the application and the Categories table will be populated with seed data
Here are the steps to seed the NorthwindCafe database:
1. Create a file call DBInitializer in the NorthwindCafe.Web "Models" 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, 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 by pressing CTRL+F5 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
Subscribe to:
Post Comments (Atom)
Search This Blog
Tags
Web Development
Linux
Javascript
DATA
CentOS
ASPNET
SQL Server
Cloud Computing
ASP.NET Core
ASP.NET MVC
SQL
Virtualization
AWS
Database
ADO.NET
AngularJS
C#
CSS
EC2
Iaas
System Administrator
Azure
Computer Programming
JQuery
Coding
ASP.NET MVC 5
Entity Framework Core
Web Design
Infrastructure
Networking
Visual Studio
Errors
T-SQL
Ubuntu
Stored Procedures
ACME Bank
Bootstrap
Computer Networking
Entity Framework
Load Balancer
MongoDB
NoSQL
Node.js
Oracle
VirtualBox
Container
Docker
Fedora
Java
Source Control
git
ExpressJS
MySQL
NuGet
Blogger
Blogging
Bower.js
Data Science
JSON
JavaEE
Web Api
DBMS
DevOps
HTML5
MVC
SPA
Storage
github
AJAX
Big Data
Design Pattern
Eclipse IDE
Elastic IP
GIMP
Graphics Design
Heroku
Linux Mint
Postman
R
SSL
Security
Visual Studio Code
ASP.NET MVC 4
CLI
Linux Commands
Powershell
Python
Server
Software Development
Subnets
Telerik
VPC
Windows Server 2016
angular-seed
font-awesome
log4net
servlets
tomcat
AWS CloudWatch
Active Directory
Angular
Blockchain
Collections
Compatibility
Cryptocurrency
DIgital Life
DNS
Downloads
Google Blogger
Google Chrome
Google Fonts
Hadoop
IAM
KnockoutJS
LINQ
Linux Performance
Logging
Mobile-First
Open Source
Prototype
R Programming
Responsive
Route 53
S3
SELinux
Software
Unix
View
Web Forms
WildFly
XML
cshtml
githu
Mua vé máy bay tại Aivivu, tham khảo
ReplyDeletesăn vé máy bay giá rẻ đi Mỹ
khi nào có vé máy bay từ mỹ về việt nam
vé máy bay từ Toronto về việt nam
vé từ nhật về việt nam
ve may bay vietnam airline tu han quoc ve viet nam
Vé máy bay từ Đài Loan về Việt Nam
chi phí vé máy bay cho chuyên gia nước ngoài