📚

Learn ASP.NET Core MVC and Entity Framework

May 11, 2025

Crash Course on ASP.NET Core MVC and Entity Framework Core

Introduction

  • Course creator: Alan Omari.
  • Focus: Learning ASP.NET Core MVC from scratch with .NET 9.
  • Topics covered include MVC architecture and working with databases using Entity Framework Core.

MVC Architecture

Definition

  • MVC stands for Model View Controller.
  • Architectural pattern that divides applications into three components: Models, Views, and Controllers.

Advantages of MVC

  • Clear separation of concerns.
  • Easier to develop, debug, and test applications.
  • Changes in one component do not affect the others.

Components Overview

  1. Model: Represents data and business logic.
    • Example: In a library management app, the model represents book data.
  2. View: Displays data to users.
  3. Controller: Handles user input and interaction, determining which model and view to work with.

Setting Up ASP.NET Core Application

Installation of .NET 9 and Visual Studio

  • Download and install .NET 9 SDK.
  • Ensure compatibility with Visual Studio 17.11.
  • Enable preview SDKs in Visual Studio settings to work with the preview version of .NET 9.

Creating an MVC Web Application

  1. Open Visual Studio and create a new project using the MVC template.
  2. Name your application and select .NET 9 as the version.
  3. Key files generated:
    • Program.cs: Entry point, sets up application configuration.
    • Controllers Folder: Contains controller classes.
    • Models Folder: Contains data models.
    • Views Folder: Contains view files (CSHTML).
    • wwwroot Folder: Contains static files like CSS and JavaScript.
    • appsettings.json: Main configuration file for settings like connection strings.

Understanding ASP.NET Core MVC Basics

Creating a Model

  • Define a model as a class (e.g., Item.cs) with properties (e.g., ID, Name).

Creating a Controller

  • Define actions within the controller to manage requests.
  • Example: An overview action to render an item details view.

Creating a View

  • Create view files (e.g., Overview.cshtml) to display data.
  • Use Razor syntax for embedding C# code into HTML.

Working with Entity Framework Core

Introduction to Entity Framework Core

  • Simplifies database access using .NET objects.
  • Supports CRUD operations: Create, Read, Update, Delete.

Approaches to Database Management

  1. Code First: Define schema using C# classes and generate a database.
  2. Database First: Generate classes from an existing database schema.

Setting Up Code First Approach

  • Install required Entity Framework Core packages.
  • Create a context class to manage database interactions.
  • Configure connection strings in appsettings.json.
  • Use migrations to create/update database schema.

Implementing CRUD Operations

  • Create actions in the controller for each CRUD operation.
  • Implement views for user interaction with these operations.

Relationships in Entity Framework Core

One-to-One Relationship

  • Example: Each item has one serial number.
  • Define models and configure relationships in the context class.

One-to-Many Relationship

  • Example: A category can have multiple items.
  • Configure relationships and update the database accordingly.

Many-to-Many Relationship

  • Example: Clients and items.
  • Use a helper model to manage the many-to-many relationship.
  • Query both models and display their connections in views.

Conclusion

  • Follow along with the tutorials for practical examples and further learning.
  • Encourage viewers to like and subscribe for more content.