Tech Junkie Blog: ASP.NET Core : Entity Framework Core Models For Northwind Cafe

Wednesday, August 3, 2016

ASP.NET Core : Entity Framework Core Models For Northwind Cafe

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.  

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
















2.  In the Product.cs file type in the following code

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:
  1. How To Create An ASP.NET Core Application From Scratch
  2. ASP.NET Core : Add jQuery, Bootstrap, AngularJS Using bower.json
  3. Enable ASP.NET Core to Serve Static Files
  4. Enable MVC On ASP.NET Core Application
  5. ASP.NET Core : Create Our First Controller and View  
  6. ASP.NET Core : Adding The Default View With _ViewStart.cshtml
  7. ASP.NET Core : Create A Responsive Layout With Bootstrap
  8. ASP.NET Core : Adding Font-Awesome For Northwind Cafe Navigation Icons
  9. ASP.NET Core : Add .json Configuration Files With Microsoft.Extensions.Configuration Library
  10. ASP.NET Core : Entity Framework Core Models For Northwind Cafe
  11. ASP.NET Core : Create The NothwindContext ( EntityFrameworkCore )
  12. ASP.NET Core : Configure project.json File To Support Entity Framework Core
  13. ASP.NET Core : Add NorthwindContext To Startup Class And Create Database
  14. ASP.NET Core: Seeding The NorwindCafe Database

No comments:

Post a Comment