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

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).

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

#Step- 2 Configure project

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

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

#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.

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

#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)
How to Create Dot Net Core web API with Entity Framework using Code First Approach (4)

#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.
How to Create Dot Net Core web API with Entity Framework using Code First Approach (5)

#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.
How to Create Dot Net Core web API with Entity Framework using Code First Approach (6)

#Step-7 Add connection string

  • In the appsettings.json file, Please add the section ConnectionStrings as shown in the image below.
How to Create Dot Net Core web API with Entity Framework using Code First Approach (7)

#Step-8 Add dependency injection of DbContext in startup

  • Now configure the dependency injection of the DbContext in the startup as shown below:
How to Create Dot Net Core web API with Entity Framework using Code First Approach (8)

#Step-9 Run the migration command

  • Open the package manager console and add the below command
  • Add-Migration <provide any name>
How to Create Dot Net Core web API with Entity Framework using Code First Approach (9)

#Step-10 Update database

  • Update-Database (run this command after successful add the migration using the above step)
How to Create Dot Net Core web API with Entity Framework using Code First Approach (10)

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

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

#Step-12 Add Controller

  • Select API > API Controller with actions, using the Entity Framework
How to Create Dot Net Core web API with Entity Framework using Code First Approach (12)

#Step-13 Select model class and DbContext

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

#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.
How to Create Dot Net Core web API with Entity Framework using Code First Approach (14)

#Step-15 Add user using API

  • Add User detail information in the request body
How to Create Dot Net Core web API with Entity Framework using Code First Approach (15)

#Step-16 Response from the API

  • Upon the successful addition of a user in the database, you can see the response from the API.
How to Create Dot Net Core web API with Entity Framework using Code First Approach (16)

#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
How to Create Dot Net Core web API with Entity Framework using Code First Approach (17)

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.

How to Create Dot Net Core web API with Entity Framework using Code First Approach (2024)
Top Articles
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 6337

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.