Modelling using EF Core in ASP.NET

Nabidul IslamNabidul Islam
5 min read

What is EF Core?

EF core, commonly known as Entity Framework core, is an open-source tool developed by Microsoft to allow developers to interact with data in the database using .NET objects instead of writing SQL queries.

What is ASP.NET?

ASP.NET is an open-source web application framework developed by Microsoft to produce web pages, applications and services using the .NET framework.

The Relationship Between EF Core and ASP.NET?

Combining these, ASP.NET allows programmers to create dynamic web applications, and EF core will enable programmers to interact with databases. This simplifies the process of data manipulation, migrations and mapping of database structures to .NET classes.

Importance of Modelling

Modelling is essential as it helps structure the application and organise how the data will be represented. A well-defined data model will let programmers understand the relationship between the data and the .NET objects, how data is manipulated, and provide a scalable application with accurate data.

DbContext in EF Core

A DbContext is a fundamental class in the EF core. It is an instance representing a session with the database. It can be used to query and save instances of your entities. Here are the main functions of DbContext:

  • Query: providing properties to entities.

  • Change tracking: track changes made to entities.

  • Saving data: SaveChanges() or SaveChangesAsync() are called to update changes made

  • Managing transactions: controls the sequence of operations performed on the database.

  • Configuration: specifying how the entity classes map to the database schema.

Entities and Database Representation in EF Core

In the EF core, entities are classes that map to a database table. Each instance of an entity represents a row in the table. Each entity contains attributes that map to the columns in the table. This precisely maps the .NET objects and the data within the database, tracks changes in each entity instance and determines the relationship between the data and .NET objects. Below is an example diagram I created from draw.io to demonstrate how the object maps to the database table. The class Car maps to the entity Car in the database table; each instance of a new Car is a row, and attributes such as the Make and Model are the columns within the database table.

Software Required

  • Docker Desktop

  • SQL Server Management Studio (SSMS)

  • Visual Studio

Setting Up Your Project

  1. Ensure you have an SQL database ready to go. I have used Docker Desktop and SSMS for this demo to create an empty 'Cars' database.

  2. Open up Visual Studio and create a new project.

  3. Search for ASP.NET Core Web Application (Razor Pages).

  4. Name your project and continue pressing Next until you see the Create option.

    Your Visual Studio will look like this once the project has been created.

Installing EF Core to Your Project

Next, we must install EF core tools onto our Visual Studio project.

  1. Right-click on the project solution. For me, it's Car_Demo.

  2. Select Manage NuGet Packages and select the browse tab.

  3. Install the following:

    • Microsoft.EntityFrameworkCore - Possess the core library for EF Core.

    • Microsoft.EntityFrameworkCore.SqlServer - Allows EF to communicate with the database.

    • Microsoft.EntityFrameworkCore.Tools - Provides useful design-time tools for EF Core, such as migrations, database updates and scaffolding.

    • Microsoft.EntityFrameworkCore.Design - Required for design-time services within EF Core, such as migrations.

Creating Your First Model

  1. In the Solution Explorer, right-click and hover over Add and select Folder.

  2. Name the folder Models and then right-click on it Add a Class, naming it Car.cs.

  1. You can provide the following attributes to your car class, and ensure all variables use appropriate data types and that nullable variables are referenced.

    Below is a snapshot of the car class model I have created.

  1. Create another folder called Data. Inside the folder, create a class called CarDbContext.

    The snapshot below shows the required code to facilitate the communication between your models and the database using DbContext.

  1. In the appsettings.json we must specify the connection string to our database. Follow the code snapshot below and input your database login details.

6 . Next, we will register CarDbContext with EF Core and configure it with our database. This allows the application to interact with the database using CarDbContext. The code snapshot is provided below.

  1. In our next steps, EF core's migration feature is to apply our latest model changes to the database. In the Package Manager Console input add-migration initial followed by update-database. Notice the changes in code & database; snapshots are provided below.

  1. Create a folder in Pages called CarMaster, then right-click and add a New Scaffold Item and select Razor Pages using EF(CRUD). Select the car model and its context and wait for the files to be generated. The generated code will allow you to create, read, update and delete records for the car model; directly through the web interface.

  2. Run the code, and it should open up on the home page. In the address bar, enter the following: https://localhost:7098/CarMaster . The web interface should look like this.

  1. Feel free to play around with the features on your interface and notice the changes to the database. I will just input some data and show you how it looks on the interface and database.

Conclusion

To wrap things up, I hope this article provided insight into the importance of modelling using EF core in ASP.NET. The demo project is simple, but it highlights the importance of EF core being a powerful tool that seamlessly facilitates the translation of complex data models from diagrams to code. By combining EF core with ASP.NET, developers can interact with databases using .NET objects instead of writing SQL queries. This is great in web development as programmers can continue building dynamic web applications while having the ability to interact with database records.

2
Subscribe to my newsletter

Read articles from Nabidul Islam directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Nabidul Islam
Nabidul Islam

Hey, I am Nabidul. I am a Software Engineering student at the University of Technology Sydney (UTS). I love gaming with my friends, going to the gym, trying new foods & exploring new places. My passion for programming started in high school, and I decided to pursue a career in Software Engineering.