Food Ordering System (Part 3): How to Create Data Models in ASP.NET Core MVC

Part 3: Understanding the Data Models

In this section, we define the core structure of our application. These C# classes (Models) represent the tables that will be created in our SQL Server database using Entity Framework Core.

Model Summaries:

  • Category: This model organizes our menu. It allows us to group food items into sections like "Fast Food," "Beverages," or "Desserts."

  • FoodItem: The heart of the shop. This model stores the name, description, image URL, and price for every dish available for order.

  • User: This handles our identity system. It stores essential profile information like usernames, email addresses, and encrypted passwords, as well as an "IsAdmin" flag to separate customers from shop managers.

  • Order: This tracks every purchase made on the platform. It records the date of the transaction, the delivery address, the total amount, and the current status (e.g., Pending or Delivered).

  • OrderItem: A critical "link" model. Since a single order can contain many different dishes, this model tracks exactly which food items were bought, in what quantity, and at what specific price.

1: Category.cs
Models/Category.cs
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace FoodOrderingSystem.Models
{
    public class Category
    {
        public int Id { get; set; }

        [Required]
        [StringLength(50)]
        public string Name { get; set; }

        public string Description { get; set; }

        public ICollection<FoodItem> FoodItems { get; set; }
    }
}
2: FoodItem.cs
Models/FoodItem.cs
using System.ComponentModel.DataAnnotations;

namespace FoodOrderingSystem.Models
{
    public class FoodItem
    {
        public int Id { get; set; }

        [Required]
        [StringLength(100)]
        public string Name { get; set; }

        public string Description { get; set; }

        [Required]
        [Range(0.01, 1000)]
        public decimal Price { get; set; }

        public string ImageUrl { get; set; }

        public int CategoryId { get; set; }
        public Category Category { get; set; }

        public bool IsAvailable { get; set; } = true;
    }
}
3: User.cs
Models/User.cs
using System.ComponentModel.DataAnnotations;

namespace FoodOrderingSystem.Models
{
    public class User
    {
        public int Id { get; set; }

        [Required]
        [StringLength(50)]
        public string Username { get; set; }

        [Required]
        [EmailAddress]
        public string Email { get; set; }

        [Required]
        public string Password { get; set; }

        public string FullName { get; set; }

        public string Address { get; set; }

        public string Phone { get; set; }

        public bool IsAdmin { get; set; } = false;
    }
}
4: Order.cs
Models/Order.cs
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace FoodOrderingSystem.Models
{
    public class Order
    {
        public int Id { get; set; }

        public int UserId { get; set; }
        public User User { get; set; }

        public DateTime OrderDate { get; set; } = DateTime.Now;

        [Required]
        public string DeliveryAddress { get; set; }

        public string PhoneNumber { get; set; }

        public decimal TotalAmount { get; set; }

        public string Status { get; set; } = "Pending";

        public ICollection<OrderItem> OrderItems { get; set; }
    }
}
5: OrderItem.cs
Models/OrderItem.cs
namespace FoodOrderingSystem.Models
{
    public class OrderItem
    {
        public int Id { get; set; }

        public int OrderId { get; set; }
        public Order Order { get; set; }

        public int FoodItemId { get; set; }
        public FoodItem FoodItem { get; set; }

        public int Quantity { get; set; }

        public decimal UnitPrice { get; set; }
    }
}

Database Relationship Table

This table explains the "Logic" of your system.

ModelPrimary KeyRelationshipConnects ToDescription
CategoryIdOne-to-ManyFoodItemOne category (e.g., "Burgers") contains many food items.
FoodItemIdMany-to-OneCategoryEach food item must belong to one specific category.
UserIdOne-to-ManyOrderOne customer can place many different orders over time.
OrderIdOne-to-ManyOrderItemOne order can contain multiple items (e.g., 2 Burgers and 1 Coke).
OrderItemIdMany-to-OneFoodItemLinks the specific food product and its price to a customer's order.

How the Data Flows (Summary)

  1. A User logs in to the system.

  2. They browse FoodItems filtered by Category.

  3. When they checkout, an Order is created.

  4. Each item they bought is saved as an OrderItem, which records the Quantity and the UnitPrice at the time of purchase.

d

Comments