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:

Prerequisites

Before starting, ensure you have the following installed:

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:

Features Implemented

// AppDbContext Code Here

Session Management

To maintain the current logged-in user during runtime, a simple session manager is used.

Responsibilities

// SessionManager Code Here

Email Validation

Before registration or login, email addresses are validated using regular expressions.

Validation Features

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

Admin Menu

User Menu

// Program.cs Code Here

Authentication Module

The authentication module is responsible for:

Auth View

The view collects user input and communicates with business services.

// AuthView Code Here

Authentication Service

The service performs:

// AuthServices Code Here

Email Notification Service

The application uses Gmail SMTP to send emails.

Responsibilities

// EmailSender Code Here

Application Messages

A centralized message class is used to manage all user-facing messages.

Benefits

// Messages Class Code Here

Result Wrapper Pattern

The application uses a generic Result Wrapper to standardize service responses.

Benefits

// Result<T> Code Here

Vehicle Rental Module

The rental module contains the core business logic of the application.

Features

Business Rules

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:

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.