Skip to main content

Introduction

SupermarketWEB uses Entity Framework Core with a set of interconnected data models that represent the core business entities. The application manages products, categories, customers, payment modes, and users through a centralized database context.

Database Context

The SupermarketContext class inherits from DbContext and provides access to all entity sets in the application:
using Microsoft.EntityFrameworkCore;
using SupermarketWEB.Models;

namespace SupermarketWEB.Data
{
    public class SupermarketContext : DbContext
    {
        public SupermarketContext(DbContextOptions options) : base(options)
        {
        }

        public DbSet<Product> Products { get; set; }
        public DbSet<Category> Categories { get; set; }
        public DbSet<Customer> Customers { get; set; }
        public DbSet<PayMode> PayModes { get; set; }
        public DbSet<User> Users { get; set; }
    }
}

Entity Relationships

The data models form a structured hierarchy with the following relationships:

Product-Category

Many-to-One relationship. Each Product belongs to one Category, and each Category can have multiple Products.

Independent Entities

Customer, PayMode, and User entities operate independently without direct foreign key relationships to other models.

Product-Category Relationship

The primary relationship in the system is between Product and Category:
  • Product contains a CategoryId foreign key and a Category navigation property
  • Category contains a Products collection navigation property
  • This establishes a one-to-many relationship where one category can contain multiple products
// Product side
public int CategoryId { get; set; }
public Category? Category { get; set; }

// Category side
public ICollection<Product>? Products { get; set; } = default!;

Core Models

Product

Product inventory with pricing, stock levels, and category classification

Category

Product categorization with hierarchical organization

Customer

Customer information including personal details and contact information

PayMode

Payment method definitions for transaction processing

User

Authentication and user account management
Navigation properties enable Entity Framework to load related data:
Navigation properties marked with ? (nullable) indicate optional relationships that may be lazy-loaded or explicitly included in queries.
Eager Loading Example:
var products = context.Products
    .Include(p => p.Category)
    .ToList();
Lazy Loading:
var product = context.Products.Find(id);
var categoryName = product.Category?.Name; // Category loaded on access

Data Annotations

The models use data annotations for:
  • Column Type Specifications: [Column(TypeName = "decimal(6,2)")] for precise decimal handling
  • Validation: [Required] for mandatory fields
  • Data Type Hints: [DataType(DataType.EmailAddress)] and [DataType(DataType.Password)] for UI rendering
Data annotations provide both database schema configuration and client-side validation rules.

Entity States

All models follow standard Entity Framework entity states:
StateDescription
AddedNew entity to be inserted
ModifiedExisting entity with changes
DeletedEntity marked for deletion
UnchangedTracked entity with no changes
DetachedEntity not tracked by context

Next Steps

Explore individual model documentation for detailed property definitions, validation rules, and usage examples:

Build docs developers (and LLMs) love