---
title: "Build a CRUD App with ASP.NET Core 3.1 & Entity Framework 3.1 Using Visual Studio 2019"
published_at: "2019-09-04T11:15:24+00:00"
modified_at: "2025-11-06T07:42:37+00:00"
url: "https://www.syncfusion.com/blogs/post/crud-app-asp-dotnet-core-entity-framework-visual-studio"
excerpt: "Let's see how to create an ASP.NET Core app and connect it to a database and perform CRUD actions using Entity Framework 3.0."
taxonomy_category:
  - "ASP.NET Core"
  - "Development"
  - "Entity Framework"
  - "Visual Studio"
  - "Web"
taxonomy_post_tag:
  - "ASP.NET Core"
  - "CRUD"
  - "Entity Framework"
  - "Visual Studio"
  - "Web Development"
---

# Build a CRUD App with ASP.NET Core 3.1 & Entity Framework 3.1 Using Visual Studio 2019

[Kannan Eswar](https://www.syncfusion.com/blogs/author/kannan-e)

![Build a CRUD App with ASP.NET Core 3.1 & Entity Framework 3.1 Using Visual Studio 2019](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Build-CRUD-application-with-ASP.NET-Core-3.0-.jpg)


**TLDR:** Building an ASP.NET Core application and establishing a database connection to execute basic CRUD operations with Entity Framework Core 3.1 using Visual Studio.

In this blog, I am going to provide a walk-through on developing a web application using ASP.NET Core 3.1. Then, I will connect it to a database (database-first) using the Entity Framework Core 3.1 command, and perform CRUD (Create, Read, Update and Delete) operations using scaffolding (code generator). I am going to develop a sample application for inventory management with basic operations.

ASP.NET Core is a web framework from Microsoft. It is an open-source, cross-platform, cloud-optimized web framework that runs on Windows using .NET Framework and .NET Core, and on other platforms using .NET Core. It is a complete rewrite that unites ASP.NET MVC and Web API into a single programming model and removes system-related dependencies. This helps in deploying applications to non-Windows servers and improves performance.

This blog post will discuss the following:

- [Prerequisites](#prerequisites)
- [Database creation](#create-database)
- [Create an ASP.NET Core application](#create-asp-dotnet-core-application)
- [Install necessary NuGet packages](#install-nuget-packages)
- [Connect application to database](#connect-applicaiton-with-database)
- [Scaffold CRUD operations](#scaffolding)
- [Run application](#run-application)
- [Conclusion](#conclusion)

**Note:** In this demo application, I have used ** ASP.NET Core 3.1, Entity Framework Core 3.1,**with ** Visual Studio 2019 16.4.0.**

## Prerequisites

A .NET Core application can be developed using these IDEs:

- [Visual Studio](https://visualstudio.microsoft.com/downloads/)
- [Visual Studio Code](https://code.visualstudio.com/)
- Command Prompt

Here, I am using Visual Studio to build the application. Be sure that the necessary software is installed:

- Visual Studio 2019 16.4.0
- NET Core 3.1
- SQL Server 2017

## Create database

Let’s create a database on your local SQL Server. I hope you have installed SQL Server 2017 in your machine (you can use SQL Server 2008, 2012, or 2016, as well).

**Step 1:** Open ** Visual Studio 2019**.

**Step 2**: Open SQL ** Server Object Explorer** and click ** Add SQL Server**.

![Adding SQL Server](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Adding-SQL-Server.png)

Adding SQL Server

**Step 3:** Here we have an option to choose from the local machine’s SQL Server, connected via network, and the Azure SQL database. I have chosen the local SQL Server instance. I provide the SQL Server details and click ** Connect**. The SQL Server will be listed in Explorer.

![Establishing connection to the database server](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Establishing-connection-to-the-database-server-1.png)

Establishing connection to the database server

![SQL Server listed in Solution Explorer](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/SQL-Server-listed-in-Solution-Explorer.png)

SQL Server listed in Solution Explorer

**Step 4:** Right-click on a database node and create a new database (** Inventory**).

**Step 5:** Now we have the database in place. Click on our database and choose ** New Query.**

**Step 6:** For this application, I am going to create a table called ** Products** with basic attributes. Paste the following SQL query into the Query window to create a ** Products** table.

```
Create Table Products(
  ProductId Int Identity(1,1) Primary Key,
  Name Varchar(100) Not Null,
  Category Varchar(100),
  Color Varchar(20),
  UnitPrice Decimal Not Null,
  AvailableQuantity Int Not Null
)
```

**Step 7:** Click the ** Run** icon to create the table. Now we have the table needed for our application.

## Create an ASP.NET Core application

Follow these steps to create an ASP.NET Core application.

**Step 1:** In Visual Studio 2019, click on ** File** -> ** New** -> ** Project**.

**Step 2:** Choose the ** Create a new project** option.

**Step 3:** Select the **ASP.NET Core Web Application** template. ![Create a new ASP.NET Core Web application](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Create-a-new-ASP.NET-Core-Web-application.png)

**Step 4:** Enter project name and click ** Create**.![Configuring the project](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Configuring-the-project.png)

**Step 5:** Select .NET Core and ASP.NET Core 3.1 and choose the ** Web Application (Model-View-Controller)** template.

Uncheck the **Configure for HTTPS** under the Advanced options (in a development environment, we have no need of SSL).

Click **Create**. Then the sample ASP.NET Core application will be created with this project structure.![Creating an ASP.NET Core application](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Entering-the-project-name.png)

## Install NuGet packages

The following NuGet packages should be added to work with the SQL Server database and scaffolding. Run these commands in **Package Manager Console**:

- **Install-Package Microsoft.VisualStudio.Web.CodeGeneration.Design -Version 3.1.4**
- **Install-Package Microsoft.EntityFrameworkCore.Tools -Version 3.1.8**
- **Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 3.1.8**

## Scaffolding

ASP.NET Core has a feature called scaffolding, which uses T4 templates to generate code of common functionalities to help keep developers from writing repeat code. We use scaffolding to perform the following operations:

- Generate entity POCO classes and a context class for the database.
- Generate code for create, read, update, and delete (CRUD) operations of the database model using Entity Framework Core, which includes **controllers** and ** views**.

## Connect application with database

Run the following scaffold command in **Package Manager Console** to reverse engineer the database to create database context and entity POCO classes from tables. The scaffold command will create POCO class only for the tables that have a primary key.

**Scaffold-DbContext “Scaffold-DbContext “Server=******;Database=Inventory;Integrated Security=True” Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models**

- **Connection—** Sets connection string of the database.
- **Provider—** Sets which provider to use to connect database.
- **OutputDir—** Sets the directory where the POCO classes are to be generated.

In our case, the Products class and Inventory context class will be created.![Product and Inventory class in the project](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Product-and-Inventory-class-in-the-project-1.png)

Open the Inventory Context class file. You will see the database credentials are hard coded in the **OnConfiguring** method.

It’s not good practice to have SQL Server credentials in C# class, considering the security issues. So, remove this **OnConfiguring** method from context file.![Remove the OnConfiguring method](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/OnConfiguring-method.png)

And move the connection string to the **appsettings.json** file.![Connection string moved to appsettings](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Connection-string-moved-to-appsettings-1.png)

Then we can register the database context service (**InventoryContext**) during application startup. In the following code, the connection string is read from the ** appsettings** file and passed to the context service.![Configuring services](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Configuring-services.png)

Then this context service is injected with the required controllers via dependency injection.

### Perform CRUD operations

Now we set up the database and configure it to work with Entity Framework Core. We’ll see how to perform CRUD operations.

Right-click on the controller folder, select **add new item**, and then select ** controller**. Then, this dialog will be displayed.![Adding MVC Controller with views](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Adding-MVC-Controller-with-views-1.png)

Select the **MVC Controller with views, using Entity Framework** option and click ** Add**.

We need to choose a database **model class** and ** data context class**, which were created earlier, and click ** Add**.![Adding model and context class](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Adding-model-and-context-calss-1.png)

That’s it, we’re done. The scaffolding engine uses T4 templates to generate code for controller actions and views in their respective folders. This is the basic version of code; we can modify it as needed.

Please find the files created,![Created class listed in the project](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Created-class-listed-in-the-project.png)

Now we have fully functional CRUD operations on the Products table.

Then, change the default application route to load the **Products Controller** instead of the home controller. Open the ** Startup.cs** file and under the Configure method, change the default controller to ** Products**.![Updating startup page](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Updating-startup-page-1.png)

With the help of the scaffolding engine, developers need not write CRUD operations for each database model.

## Run application

Click **Run** to view the application. A new browser tab will open and we’ll be able to see the product listing page. Since there is no product in the inventory, it’s empty.![Empty Product Listing](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Empty-Product-Listing-1.png)

### Create

Click **Create New** to add new products to the inventory.![Adding new product to inventory](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Adding-new-product-to-inventory-1.png)

After entering the details, click **Create**. Now we should see newly created products in the listing page as in the following screenshot. I have added three more products.![Newly created products](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Product-Listing-updated-1.png)

### Read

Click **Details** to view the product details.![Details of the product](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Details-of-the-product-2.png)

### Update

Click **Edit** to update product details.![Editing the product](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Editing-the-product-2.png)

### Delete

Click **Delete** to delete a product. Confirmation will be requested before it’s deleted from the database.![Deleting the product](https://www.syncfusion.com/blogs/wp-content/uploads/2019/09/Deleting-the-product-1.png)

Without writing a single line of code, we are able to create an application with basic CRUD operations with the help of the scaffolding engine.

I have shared the sample application in [this GitHub location](https://github.com/SyncfusionExamples/inventory-management-asp-dotnet-core)
. Extract the application, change the connection string in the **appsettings.json** file that points to your SQL Server, and run the application.

## Conclusion

In this blog, we have learned how to create an ASP.NET Core application and connect it to a database to perform basic CRUD operations using Entity Framework Core 3.1 and a code generation tool. I hope it was useful. Please share your feedback in the comments section below.

The [Syncfusion ASP.NET Core UI controls library](https://www.syncfusion.com/aspnet-core-ui-controls)
 is the only suite that you will ever need to build an application since it contains over 65 high-performance, lightweight, modular, and responsive UI controls in a single package. Download our free trial from [here](https://www.syncfusion.com/downloads/aspnetcore-js2)
. You can also explore our online demos [here](https://ej2.syncfusion.com/home/aspnetcore.html)
.

If you have any questions or require clarifications about these controls, please let us know in the comments below. You can also contact us through our [support forum](https://www.syncfusion.com/forums)
, [support portal](https://support.syncfusion.com/)
, or [feedback portal](https://www.syncfusion.com/feedback/aspnet-core)
. We are happy to assist you!

## Recommended resources

- [Visual Studio 2022 Succinctly](https://www.syncfusion.com/succinctly-free-ebooks/visual-studio-2022-succinctly)
- [Simple Steps to Automate UI Testing in ASP.NET Core](https://www.syncfusion.com/blogs/post/simple-steps-to-automate-ui-testing-in-asp-net-core.aspx)
- [Creating an ASP.NET Core CRUD Web API with Dapper and PostgreSQL](https://www.syncfusion.com/blogs/post/creating-an-asp-net-core-crud-web-api-with-dapper-and-postgresql.aspx)
- [Easy Steps to Migrate an ASP.NET MVC Project to an ASP.NET Core Project](https://www.syncfusion.com/blogs/post/migrate-asp-net-mvc-project-to-asp-net-core.aspx)
- [Simple Steps to Integrate a Blazor WebAssembly Project with an Existing ASP.NET Core Application](https://www.syncfusion.com/blogs/post/integrate-blazor-webassembly-in-asp-net-core-application.aspx)
