How to Create Dot Net Core web API with Entity Framework using Code First Approach

How to Create dot net core web api with entity framework using code first approach

Code First is a strategy that allows us to construct, move, and maintain databases and tables entirely from code. The term “from the code” refers to the fact that you may maintain the database and its associated tables directly from the.NET code. It comes in handy when you don’t have a database and want to start working on a new project and want to establish and manage a database directly from your code.

We can always use the migration process to assist us to create, update, and sync the database with your model classes. We’ll walk through the Entity Framework Core migration step by step in this example. This blog will explain what the Code First strategy is and how we can use Entity Framework Core migration to achieve it in ASP.NET Core apps. So, let’s have a look at how to accomplish it step by step.

The Entity Framework

Microsoft’s Entity Framework is an open-source ORM framework for.NET applications. It allows developers to deal with data using domain-specific objects rather than the underlying database tables and columns where the data is kept. When working with data, developers may use the Entity Framework to work at a higher level of abstraction, allowing them to design and manage data-oriented applications with less code than traditional programs.

Reasons to choose ASP .NET Core

ASP.NET Core has a lot of advantages over ASP.NET MVC/Web API. To begin with, there is now only one framework rather than two. It appeals to most people since it is convenient and reduces ambiguity. Second, we have logging and DI containers without the need for any extra libraries, which saves me time and enables me to focus on developing better code rather than evaluating and picking the best libraries.

Pre-requisites for the project

  • ASP.NET and web development workload in Visual Studio 2019 16.4 or later
  • .NET Core 3.1 SDK or later is required.

Steps to Creating the API

You need to follow the given steps to create the web API using the code first approach:

#Step – 1 Create a new ASP .NET project

Select ASP.NET Core web application from the list (as shown below).

Create a new project

#Step- 2 Configure project

In the next step, you need to configure your project and specify its name and location.

#Step -3 Select ASP.NET Core Web API with ASP.Net Core 5.0 version 

Now, in the configuration window, choose your API and version.

Web application

#Step-4 Add NuGet package

For executing database operations from the code, we’ll use the NuGet Package Manager to install some of the needed Entity Framework Core components. So, you need to install the following Entity Framework Dependencies.

  1. Microsoft.EntityFrameworkCore.SqlServer (5.0.6)
  2. Microsoft.EntityFrameworkCore.Tools (5.0.6)
NuGet package

#Step-5 Add Entity Model Class

  • Here the Entity Model class is UserMaster.cs
  • For using the code-first approach, we need to provide all information while we are doing the same process from the SQL server.
  • For example, we need to add column name, column size, column type, relation with another table, set the primary key, auto-increment id, etc.
Entity Model Class

#Step-6 Add DB context

  • Add a new class as shown in the image below.
  • Because a DbContext object represents a database session and may be used to query and save instances of your entities, we need to inherit it. DbContext is a combination of the Repository patterns & Unit of Work.
Add DB context

#Step-7 Add connection string

  • In the appsettings.json file, Please add the section ConnectionStrings as shown in the image below.
connection string

#Step-8 Add dependency injection of DbContext in startup

  • Now configure the dependency injection of the DbContext in the startup as shown below:

#Step-9 Run the migration command

  • Open the package manager console and add the below command
  • Add-Migration <provide any name>  
Run the migration command

#Step-10 Update database

  • Update-Database (run this command after successful add the migration using the above step)
Update Database

#Step-11 After successful migration command run, check the SQL database

SQL database

#Step-12 Add Controller

  • Select API > API Controller with actions, using the Entity Framework
Controller

#Step-13 Select model class and DbContext

DbContext

#Step-14 Using Scaffolding create all API action methods

  • ASP Dot NET Core creates all endpoints for the Add User, Update User, Delete User, Select User, or list of users.

#Step-15 Add user using API

  • Add User detail information in the request body

#Step-16 Response from the API

  • Upon the successful addition of a user in the database, you can see the response from the API.
API Response

#Step-17 View in Database

  • “Select * from UserMasters” -> run this query in the database
  • We can see that the user is added in table UserMasters
View in Database

Conclusion

So, in this blog, we had seen how to implement the Code First Approach in an ASP .NET project using Entity framework Core migration. With this approach, you can perform updating and deleting users using the respective API from the listed ones. It is hoped that the information was helpful to you.

The following two tabs change content below.

Leave a Reply

Your email address will not be published. Required fields are marked *