Wednesday, August 3, 2016
In the previous post we added a configuration file call appSettings.json file to store our connection string to the database that we are going to create through Entity Framework. Even though Microsoft provides us with the Northwind database, we don't really want to use it because it's outdated. We care going to modernize the database by rebuilding it from scratch with the code first approach with Entity Framework Core. If you look at the existing Northwind database you will see that there's a lot of redundant data and tables. For example there are tables for Customers, Employees, Suppliers and Shippers. Those are basically roles, and we will take care of those roles later on in the series using the Identity framework. What we are going to do is start out simple with just the Products, Categories, Orders, OrderDetails table and add on to those tables as we progress in building the application.
The first thing we need to do is create the models for the objects that we need.
2. In the Product.cs file type in the following code
The code above gives the Product class properties Id, Name, Description, and Price. Those maps to what will become the columns in the database.
The line
Tells Entity Framework that there is a foreign key constraint between the Product table and the Category table. Basically there's a one-to-many relationship between the Product and the Category table. Meaning there could be many products to one category.
3. In the Category.cs file type in the following code
Since a category can have more one product in our database, there's a navigation property in Category entity. This means that the category entity can navigate (access, query) all the products that belongs to it. That's the power of entities, you don't have to do a database join to get products that belongs to a particular category.
4. In the Order.cs file type in the following code
5. In the OrderDetails.cs file type in the following code
The code above has the same concept, the OrderDetails entity has a foreign constraint to the Product and Order table/entity, and the Order table/entity can navigate to the OrderDetails table/entity.
The first thing we need to do is create the models for the objects that we need.
Here are the steps to create models for the Northwind Cafe entities:
1. In "Models" folder create four files
- Product.cs
- Order.cs
- Category.cs
- OrderDetail.cs
using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace NorthwindCafe.Web.Models { public class Product { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public decimal Price { get; set; } public virtual Category Category { get; set; } } }
The code above gives the Product class properties Id, Name, Description, and Price. Those maps to what will become the columns in the database.
The line
public virtual Category Category { get; set; }
Tells Entity Framework that there is a foreign key constraint between the Product table and the Category table. Basically there's a one-to-many relationship between the Product and the Category table. Meaning there could be many products to one category.
3. In the Category.cs file type in the following code
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace NorthwindCafe.Web.Models { public class Category { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public virtual ICollection<Product> Products { get; set; } } }
Since a category can have more one product in our database, there's a navigation property in Category entity. This means that the category entity can navigate (access, query) all the products that belongs to it. That's the power of entities, you don't have to do a database join to get products that belongs to a particular category.
4. In the Order.cs file type in the following code
using System.Linq; using System.Threading.Tasks; namespace NorthwindCafe.Web.Models { public class Order { public int Id { get; set; } public DateTime OrderDate { get; set; } public virtual ICollection<OrderDetail> OrderDetails { get; set; } } }
5. In the OrderDetails.cs file type in the following code
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace NorthwindCafe.Web.Models { public class OrderDetail { public int Id { get; set; } public int Quantity { get; set; } public decimal Price { get; set; } public virtual Product Product { get; set; } public virtual Order Order { get; set; } } }
The code above has the same concept, the OrderDetails entity has a foreign constraint to the Product and Order table/entity, and the Order table/entity can navigate to the OrderDetails table/entity.
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
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
ReplyDeletevé máy bay đi Mỹ giá bao nhiêu
chuyến bay từ mỹ về việt nam tháng 1/2021
vé máy bay từ đức về việt nam
vé máy bay từ việt nam sang nga bao nhiêu
bay từ anh về việt nam
ve may bay tu phap ve viet nam
Chuyen bay cho chuyen gia nuoc ngoai