CRUD Operations in .NET Core & .NET Framework Upgrade – Session 48

CRUD Repository & .NET Framework Upgrade - Session48

In this session, we are going to implement CRUD operations in ASP.NET Core MVC Web app, also we upgrade our web app .NET framework to use the latest released version. During this session, we complete the Cost model Interface and repository to handle CRUD operations. CRUD stands for Create, Read, Update and Delete operations that are four basic operations of storage which could be memory or database. Thus, for creating, reading, updating, and deleting data from storage we need a repository that handles all the mentioned verbs.

What is CRUD operations?

Generally, in programming and database, CRUD stands for Create, Read, Update and Delete are four basic operations that should be implemented in each persistent storage application. On the other hand, each persistent storage application should have the ability to create a new record, read, update and delete the existed data.

Implement Repository to Hande CRUD Operations in ASP.NET Core MVC

During sessions fifteen and sixteen, we discussed in detail about Interface, Repository, and Dependency Injection. In this session, we work again with the Cost Repository to cover all CRUD functions. Thus, first, we need to add the required Methods to ICostRepositoty.

As you can see, we added methods that perform Create, Read, Update and Delete functions in ICostRepository.

Next, We need to implement these methods in the Repositories. Now we have just StaticCostRepository which works with Static Data.

First, create a new record.

Second, Read all the Cost records and a record with a specific ID.

Third, update a specific record.

Finally, delete a record with a specific ID.

Also, you can check all the code which is related to StaticCostRepository which handles CRUD operations.

Create Repository to Handle CRUD Operations in SQL Server

For creating this repository, first, we need to add the Cost model as a DbSet property to our DbContext class.

Then, we need to add a Class inside the Models folder which should be implemented from ICostRepository. Also, we should inject DbConetxt class to this Repository with Constructor.

Thereupon, we should implement all the methods that we declared in ICostRepository.

Create a new record in SQL Server Database.

Read all the Cost records and a specific Cost record from SQL Server DB.

We can implement the update function with two methods.

Method 1: Finding the record and update the fields that we need.

Method 2: Attaching the updated data and changing the data state.

Also, you can review all the SQLServerCostRepository below.

Switching Between Repositories

As we described this topic in previous sessions, we can switch between the repositories by changing the active repository in Startup Class. Having said that, we can easily switch between them because of the Dependency Injection that we implemented in our project.

Upgrade .NET Framework

For upgrading .NET Framework, we need to meet its requirement. For instance, for upgrading the .NET framework of an ASP.NET MVC Web app from version 5 to version 6 you can check this LINK. Also, the requirements are different based on OS and IDE versions.

Upgrade .NET framework from V 5.0 to V 6.0

As you can see in the above figure, for upgrading the Web App .NET framework in different situations, we need to update and install some tools and features. Since we use Microsoft Windows and OS and Visual Studio as ID, we need to set up Visual Studio to upgrade our Web App .NET framework from 5.0 to 6.0.

Then, we can open our project with Visual Studio 2022 and change the Target Framework to version 6.0 from Application-General in the application properties.

Web App Properties in Visual Studio 2022

Also for more compatibility, It is recommended to upgrade all NuGet Packages to the version which is compatible with the target framework.

If you need more details, watch this session video. Also, for being updated about our coming sessions, follow us on Instagram, Facebook, Telegram, or YouTube. Moreover, you can have access to the list of all sessions HERE and you can download this session source code from our GitHub.

You can download this Session Slides form HERE.

0.00 avg. rating (0% score) - 0 votes

Tags

0 0 votes
Article Rating
guest

0 Comments
Inline Feedbacks
View all comments