Introduction
In this article, we will build a Vehicle Rental Management System using C#, Entity Framework Core, SQL Server, and a layered architecture approach. The application demonstrates authentication, vehicle rental management, customer registration, session handling, email notifications, and database operations using Entity Framework Core.
The project follows a multi-layered architecture consisting of:
DAL (Data Access Layer)
BLL (Business Logic Layer)
UI Layer
Shared Utilities
Prerequisites
Before starting, ensure you have the following installed:
.NET SDK
SQL Server
Visual Studio
Entity Framework Core Tools
Required NuGet Packages
Install the following packages:
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.EntityFrameworkCore.Design
Configuration Packages
Install-Package Microsoft.Extensions.Configuration
Install-Package Microsoft.Extensions.Configuration.Json
Install-Package Microsoft.Extensions.Configuration.FileExtensions
Install-Package Microsoft.Extensions.FileProviders.Physical
Console UI Packages
Install-Package Spectre.Console
Install-Package ConsoleTables
Database Commands
Add-Migration InitialCreate
Update-Database
Project Structure
The solution is organized into the following layers:
DAL
├── Context
├── Entities
├── Repository
BLL
├── Interfaces
├── Services
UI
├── Views
Shared
├── Constants
├── Enums
├── ResultWrapper
Utilities
├── Validators
├── SessionManager
├── EmailSender
Database Configuration
The application uses Entity Framework Core with SQL Server.
appsettings.json
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=DELL,51434;Initial Catalog=Ayush;Persist Security Info=True;User ID=Ayush;Password=Ayush;Encrypt=True;TrustServerCertificate=True;"
}
}
Application DbContext
The AppDbContext is responsible for:
Database connectivity
Entity relationships
Seed data
Table mappings
Features Implemented
User ↔ Customer One-to-One Relationship
Customer ↔ Rental One-to-Many Relationship
Vehicle ↔ Rental One-to-Many Relationship
Seeded Users
Seeded Vehicles
// AppDbContext Code Here
Session Management
To maintain the current logged-in user during runtime, a simple session manager is used.
Responsibilities
Store logged-in user
Logout user
Check login status
Verify admin access
// SessionManager Code Here
Email Validation
Before registration or login, email addresses are validated using regular expressions.
Validation Features
Empty email check
Email format validation
Password presence validation
// EmailValidator Code Here
Console Application Entry Point
The application starts from Program.cs.
The menu system is built using Spectre.Console and provides:
Public Menu
Login
Register
Exit
Admin Menu
View Active Rentals
View Paused Rentals
Logout
User Menu
View Available Vehicles
Rent Vehicle
Return Vehicle
View Rentals
Logout
// Program.cs Code Here
Authentication Module
The authentication module is responsible for:
User Registration
User Login
Session Creation
Auth View
The view collects user input and communicates with business services.
// AuthView Code Here
Authentication Service
The service performs:
Email validation
Password validation
Existing user checks
Customer registration
Session creation
// AuthServices Code Here
Email Notification Service
The application uses Gmail SMTP to send emails.
Responsibilities
Send confirmation emails
Send notifications
Deliver OTP messages
// EmailSender Code Here
Application Messages
A centralized message class is used to manage all user-facing messages.
Benefits
Easy maintenance
Reusable constants
Consistent user communication
// Messages Class Code Here
Result Wrapper Pattern
The application uses a generic Result Wrapper to standardize service responses.
Benefits
Success status tracking
Message handling
Data transportation
// Result<T> Code Here
Vehicle Rental Module
The rental module contains the core business logic of the application.
Features
View Available Vehicles
Rent Vehicles
Return Vehicles
View Rental History
Rental Validation Rules
Business Rules
Rental duration must be between 1 and 30 days.
A customer cannot have more than 2 active rentals.
A customer cannot rent the same vehicle twice simultaneously.
Vehicle inventory is automatically updated.
User Services
The UserServices class handles all rental operations.
// UserServices Code Here
Database Migration
After configuring the entities and context, execute the following commands:
Add-Migration InitialCreate
Update-Database
This will:
Create the database
Generate tables
Seed default users
Seed vehicle records
Application Flow
Registration Flow
User enters registration details.
Email is validated.
User record is created.
Customer record is created.
Success message is returned.
Login Flow
User enters credentials.
Email validation occurs.
Password verification is performed.
Session is created.
User gains access to the application.
Vehicle Rental Flow
User views available vehicles.
User selects a vehicle.
Business rules are validated.
Rental record is created.
Vehicle inventory is updated.
Vehicle Return Flow
User selects active rental.
Rental status changes to Returned.
Vehicle inventory increases.
Rental record is updated.
Conclusion
This Vehicle Rental Management System demonstrates how to build a layered .NET application using Entity Framework Core, SQL Server, Spectre.Console, and clean service-based architecture. The solution covers authentication, customer management, vehicle rentals, session management, email integration, and database operations while maintaining clear separation of concerns between layers.
Join the conversation! Your thoughts help the community grow.