Database Schema
Relevant source files
The following files were used as context for generating this wiki page:
- Migrations/UserDBContextModelSnapshot.cs
- Configuration/CategoryConfiguration.cs
- Migrations/20250108150417_AddResetTokenToUser.Designer.cs
- Models/Admin.cs
- Data/UserDBContext.cs
- Seeders/DatabaseSeeder.cs
- Configuration/AdminConfiguration.cs
- Models/Category.cs
- Program.cs
- Configuration/ProductConfiguration.cs
- Data/UserDBContextFactory.cs
- Migrations/20250104092740_New.cs
- Dtos/CreateOrderRequest.cs
- Dtos/UpdateOrderRequest.cs
The database schema for this project is defined and managed using Entity Framework Core. It is designed to support an e-commerce application, handling entities such as users, administrators, products, categories, and orders. The UserDBContext class serves as the primary entry point for database operations, orchestrating the configuration and relationships between the different data models.
All entity configurations are applied within the OnModelCreating method of the UserDBContext, ensuring a consistent and centralized approach to defining the database structure, constraints, and relationships. The schema also includes initial data seeding for the User table.
Entity Relationship Diagram
The following diagram illustrates the relationships between the main entities in the database.
Entities and Relationships
This section details the schema for each database table.
User
The User table stores customer account information, including credentials and roles. It also contains fields for password reset functionality.
| Field | Type | Constraints | Description |
|---|---|---|---|
Id | int | Primary Key, Identity(1,1) | Unique identifier for the user. |
Email | nvarchar(100) | Required | User's email address. |
UserName | nvarchar(50) | Required | User's chosen username. |
Password | nvarchar(256) | Required | Hashed password for the user. |
Role | bit | Required | Indicates if the user has a special role (e.g., admin-level access). |
ResetToken | nvarchar(max) | Nullable | Token for password reset requests. |
ResetTokenExpiration | datetime2 | Nullable | Expiration date for the password reset token. |
Sources: Migrations/UserDBContextModelSnapshot.cs:218-254, Seeders/DatabaseSeeder.cs:11-21
Admin
The Admins table stores information for system administrators.
| Field | Type | Constraints | Description |
|---|---|---|---|
AdminID | int | Primary Key, Identity(1,1) | Unique identifier for the admin. |
FullName | nvarchar(150) | Required | The full name of the administrator. |
Email | nvarchar(100) | Required | Administrator's email address. |
Password | nvarchar(256) | Required | Hashed password for the administrator. |
IsAdmin | bit | Required | Flag indicating administrative privileges. |
Sources: Models/Admin.cs, Configuration/AdminConfiguration.cs:11-29, Migrations/UserDBContextModelSnapshot.cs:29-55
Category
The Categories table is used to group products.
| Field | Type | Constraints | Description |
|---|---|---|---|
Id | int | Primary Key, Identity(1,1) | Unique identifier for the category. |
Name | nvarchar(100) | Required | The name of the category. |
Description | nvarchar(500) | Required | A description of the category. |
Sources: Models/Category.cs, Configuration/CategoryConfiguration.cs:11-22, Migrations/UserDBContextModelSnapshot.cs:61-80
Product
The Product table contains details about the items available for sale. It has a nullable foreign key to the Categories table.
| Field | Type | Constraints | Description |
|---|---|---|---|
Id | int | Primary Key, Identity(1,1) | Unique identifier for the product. |
Title | nvarchar(100) | Required | The title of the product. |
Name | nvarchar(200) | Required | The full name of the product. |
Description | nvarchar(500) | Required | A detailed description of the product. |
Gender | nvarchar(10) | Required | Target gender for the product (e.g., 'Male', 'Female'). |
Price | decimal(18,2) | Required | The price of the product. |
ProductImageUrl | nvarchar(500) | Required | URL for the product's image. |
Url | nvarchar(200) | Required | A URL-friendly slug for the product page. |
CategoryId | int | Nullable, Foreign Key | Links to the Categories table. |
Sources: Configuration/ProductConfiguration.cs:11-39, Migrations/UserDBContextModelSnapshot.cs:167-212
Order
The Orders table stores information about customer orders. Each order is linked to a User.
| Field | Type | Constraints | Description |
|---|---|---|---|
OrderID | int | Primary Key, Identity(1,1) | Unique identifier for the order. |
Address | nvarchar(200) | Required | Shipping address for the order. |
CustomerID | int | Required, Foreign Key | Links to the User who placed the order. |
DeliveryMethod | nvarchar(50) | Nullable | The chosen method for delivery. |
OrderCode | nvarchar(100) | Nullable | A unique code for the order. |
OrderDate | datetime2 | Required, Default GETDATE() | The date and time the order was placed. |
PaymentMethod | nvarchar(50) | Nullable | The method used for payment. |
Status | nvarchar(50) | Required | The current status of the order (e.g., 'Pending'). |
TotalAmount | decimal(18,2) | Required | The total cost of the order. |
Sources: Migrations/UserDBContextModelSnapshot.cs:86-128, Dtos/CreateOrderRequest.cs
OrderItem
The OrderItems table is a junction table that links Orders and Products, representing the specific items included in an order.
| Field | Type | Constraints | Description |
|---|---|---|---|
Id | int | Primary Key, Identity(1,1) | Unique identifier for the order item entry. |
OrderID | int | Required, Foreign Key | Links to the Orders table. |
ProductID | int | Required, Foreign Key | Links to the Product table. |
Quantity | int | Required | The number of units of the product ordered. |
UnitPrice | decimal(18,2) | Required | The price of a single unit at the time of purchase. |
Sources: Migrations/UserDBContextModelSnapshot.cs:134-161, Dtos/CreateOrderRequest.cs:19-24
Database Context Configuration
The UserDBContext class is responsible for configuring the database model. It uses the OnModelCreating method to apply entity-specific configurations from separate classes and to define relationships.
// Source: Data/UserDBContext.cs:15-32
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// Apply all configurations
modelBuilder.ApplyConfiguration(new UserConfiguration());
modelBuilder.ApplyConfiguration(new OrderConfiguration());
modelBuilder.ApplyConfiguration(new ProductConfiguration());
modelBuilder.ApplyConfiguration(new AdminConfiguration());
modelBuilder.ApplyConfiguration(new CategoryConfiguration()); // Add this line
modelBuilder.ApplyConfiguration(new OrderItemConfiguration());
modelBuilder.Entity<Order>()
.HasMany(o => o.OrderItems)
.WithOne(oi => oi.Order)
.HasForeignKey(oi => oi.OrderID);
modelBuilder.Entity<OrderItem>()
.HasOne(oi => oi.Product)
.WithMany()
.HasForeignKey(oi => oi.ProductID);
// Apply any seed data if necessary
modelBuilder.Seed();
}
This method demonstrates how Fluent API configurations are loaded and how relationships like the one between Order and OrderItem are explicitly defined.
Sources: Data/UserDBContext.cs:15-32
Data Seeding
The database is seeded with an initial user account upon creation. This is handled by the DatabaseSeeder extension method, which is called from OnModelCreating.
// Source: Seeders/DatabaseSeeder.cs:9-22
public static void Seed(this ModelBuilder modelBuilder)
{
_ = modelBuilder.Entity<User>().HasData(
new User()
{
Id = 1,
Email = "chihtt123@gmail.com",
UserName = "chi",
Password = "123",
Role = true
}
);
}
Sources: Seeders/DatabaseSeeder.cs:9-22, Data/UserDBContext.cs:31