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:

PowerShell
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
Install-Package Microsoft.EntityFrameworkCore.Design

Configuration Packages

PowerShell
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

PowerShell
Install-Package Spectre.Console
Install-Package ConsoleTables

Database Commands

PowerShell
Add-Migration InitialCreate
Update-Database

Project Structure

The solution is organized into the following layers:

Code
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

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

C#
// 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

C#
// 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

C#
// 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

C#
// 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.

C#
// AuthView Code Here

Authentication Service

The service performs:

  • Email validation

  • Password validation

  • Existing user checks

  • Customer registration

  • Session creation

C#
// AuthServices Code Here

Email Notification Service

The application uses Gmail SMTP to send emails.

Responsibilities

  • Send confirmation emails

  • Send notifications

  • Deliver OTP messages

C#
// 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

C#
// 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

C#
// 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.

C#
// UserServices Code Here

Database Migration

After configuring the entities and context, execute the following commands:

PowerShell
Add-Migration InitialCreate
Update-Database

This will:

  • Create the database

  • Generate tables

  • Seed default users

  • Seed vehicle records

Application Flow

Registration Flow

  1. User enters registration details.

  2. Email is validated.

  3. User record is created.

  4. Customer record is created.

  5. Success message is returned.

Login Flow

  1. User enters credentials.

  2. Email validation occurs.

  3. Password verification is performed.

  4. Session is created.

  5. User gains access to the application.

Vehicle Rental Flow

  1. User views available vehicles.

  2. User selects a vehicle.

  3. Business rules are validated.

  4. Rental record is created.

  5. Vehicle inventory is updated.

Vehicle Return Flow

  1. User selects active rental.

  2. Rental status changes to Returned.

  3. Vehicle inventory increases.

  4. 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.