How to Add Stored Procedures in ASP.NET Core Apps Using SPToCore
Tags: webdev, dotnet, csharp, productivity
Published: 2020-11-02 10:33:23

---
title: How to Add Stored Procedures in ASP.NET Core Apps Using SPToCore
published: true
date: 2020-10-27 10:00:54 UTC
tags: webdev, dotnet, csharp, productivity
canonical_url: https://www.syncfusion.com/blogs/post/how-to-add-stored-procedures-in-asp-net-core-apps-using-sptocore.aspx
cover_image: https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/How-to-Add-Stored-Procedures-in-ASP.NET-Core-Apps-Using-SPToCore-672x372.png
---
[Stored procedures](https://en.wikipedia.org/wiki/Stored_procedure) are pieces of reusable code that you can save in a database data dictionary. They help you extract, edit, and remove data from a database without writing the code to do so again and again. They save you time, reduce workloads, and increase productivity.

In this blog, we will see how to add stored procedures to an ASP .NET Core application using the SPToCore tool.

This blog is organized into the following sections:

- [Prerequisites](#prerequisites)
- [Download SPToCore tool](#download-the-sptocore-tool)
- [Get stored procedure class file](#get-stored-procedure-class-file)
- [Add stored procedures in .NET Core application](#how-to-add-a-stored-procedure-in-a-net-core-applicaion)
- [Notes](#notes)
- [Conclusion](#conclusion)

Let’s get started!

## Prerequisites

The following items should be installed in your machine.

- [.NET Core SDK](https://dotnet.microsoft.com/download/dotnet-core/3.1)
- [Visual Studio 2019](https://visualstudio.microsoft.com/downloads/)

## Download the SPToCore tool

Download and extract the SPToCore tool from its [GitHub location](https://github.com/DarioN1/SPToCore).

## Get stored procedure class file

After downloading the SPToCore tool, we need to get the stored procedure class file.

1. Open the SPToCore tool using Visual Studio as shown in the following the screenshot.  
 ![Open the SPToCore tool using Visual Studio](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/Open-the-SPToCore-tool-using-Visual-Studio.png)
2. Open the **program.cs** file in the Solution Explorer. It will look like the following screenshot.  
 ![Open the program.cs file in the Solution Explorer](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/Open-the-program.cs-file-in-the-Solution-Explorer.png)
3. In the code editor, enter your parameters as shown in the following screenshots.  
 ![Enter your parameters in the code editor](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/Enter-your-parameters-in-the-code-editor.png)  
**Parameter Details**  
The following parameter details are reproduced from the [SPToCore GitHub README](https://github.com/DarioN1/SPToCore#params).  
 ![Parameter Details](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/Parameter-Details.png)
4.  After entering the parameters, run the SPToCore tool and it will open the following window.  
 ![Microsoft Visual Studio Debug Console Window](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/Microsoft-Visual-Studio-Debug-Console-Window.png)
5. After completing the tool execution, check the **output** folder to ensure it is as you specified in the parameter.  
 ![Check the output folder to ensure it is as you specified in the parameter](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/Check-the-output-folder-to-ensure-it-is-as-you-specified-in-the-parameter.png)
6. Open the automatically generated class file from the **output** folder in Visual Studio and it’ll show you the following code.  
 ![Open the automatically generated class file from the output folder in Visual Studio](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/Open-the-automatically-generated-class-file-from-the-output-folder-in-Visual-Studio.png)

Now, you can attach this automatically generated class into your .NET Core application by following the steps in the next section.

## How to add a stored procedure in a .NET Core application

1. Open Visual Studio and create an ASP.NET Core Blazor application. Refer to the following screenshot.  
 ![create an ASP.NET Core Blazor application](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/create-an-ASP.NET-Core-Blazor-application.png)
2. Create a folder and name it **Entity**.  
 ![Create a folder and name it Entity](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/Create-a-folder-and-name-it-Entity.png)
3. Then, copy your automatically generated class file and paste it in the Entity folder as shown below.  
 ![Copy your automatically generated class file and paste it in the Entity folder](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/Copy-your-automatically-generated-class-file-and-paste-it-in-the-Entity-folder.gif)
4. Then, run the following commands in the package manager console to install the necessary NuGet packages.  

| `PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer
PM> Install-Package Microsoft.EntityFrameworkCore
` |

5. Register your Entity class in the **ConfigureServices** method present in the **startup** class. Refer to the following screenshot.  
 ![Register your Entity class in the ConfigureServices method present in the startup class](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/Register-your-Entity-class-in-the-ConfigureServices-method-present-in-the-startup-class.png)
6. Then, inject the **IConfiguration** service into the automatically generated class using the automatically generated class constructor. This IConfiguration service is used to read configuration data from key-value pairs employing a sort of configuration source.  
 ![Inject the IConfiguration service into the automatically generated class](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/Inject-the-IConfiguration-service-into-the-automatically-generated-class.png)
7. Override the **OnConfiguring** method by passing the following code in the automatically generated class file.
```csharp
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
 {
optionsBuilder.UseSqlServer(_configuration.GetConnectionString("DBConnection"));
 }
```

In this method, we should provide a connection string. There are two ways to add a connection string:  
1. Hard-coded type.  
2. Get connection string from appsettings.json file.

We should not hard-code the connection string as this approach has some security issues. So, I recommend you get the connection string from the appsettings.json file.

### How to add connection string

Enter your connection string in the appsettings.json file as shown below.

**Note:** Verify the connection string in the **appsettings.json** file and **OnConfiguring** method are the same.

![Enter your connection string in the appsettings.json file](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/Enter-your-connection-string-in-the-appsettings.json-file.png)

8. Finally, the stored procedure is added in your .NET Core application. You can execute the stored procedure by calling the **async()** method present in the automatically generated class file.

**Note: ** A sample stored procedure calling is shown below.

![sample stored procedure calling](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/sample-stored-procedure-calling-1.png)  
 ![sample stored procedure calling](https://www.syncfusion.com/blogs/wp-content/uploads/2020/10/sample-stored-procedure-calling-2.png)

## Notes

If you would like to update the whole database, use the following command.

| `Scaffold-DbContext -Connection name=MyConnection -Provider Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context MyDbContext -Force
` |

If you would like to update a target table, use the following command.

| `Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir DirectoryNameOfYourModels -Tables employee -f
` |

If you’re using .NET Core CLI, then use the following command.

| `dotnet ef dbcontext scaffold "Server=(localdb)\mssqllocaldb;Database=DatabaseName;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o DirectoryNameOfYourModels -t employee -f
` |

## Conclusion

In this blog, we learned how to generate a stored procedure class using the SPToCore tool and how to incorporate the stored procedure in an ASP.NET Core application. With this you can retrieve, edit, and remove data from a database easily without the need to write the code again and again. This will definitely save you time, reduce your workload, and increase your productivity.

With over 70 components, our [ASP.NET Core](https://www.syncfusion.com/aspnet-core-ui-controls) toolkit powered by [Essential JS 2](https://www.syncfusion.com/javascript-ui-controls) contains all you need for building line-of-business applications, including popular widgets such as a [DataGrid](https://www.syncfusion.com/aspnet-core-ui-controls/grid), [Charts](https://www.syncfusion.com/aspnet-core-ui-controls/charts), [Gantt Chart](https://www.syncfusion.com/aspnet-core-ui-controls/gantt-chart), [Diagram](https://www.syncfusion.com/aspnet-core-ui-controls/diagram), [Spreadsheet](https://www.syncfusion.com/aspnet-core-ui-controls/spreadsheet), [Scheduler](https://www.syncfusion.com/aspnet-core-ui-controls/scheduler), and more. Use them to jump-start your development!

For existing customers, the latest version is available for download from the [License and Downloads](https://www.syncfusion.com/account/downloads) page. If you are not yet a Syncfusion customer, you can try our 30-day [free trial](https://www.syncfusion.com/downloads) to check out the available features. Also, you can try our samples from this [GitHub](https://github.com/syncfusion) location.

If you wish to send us feedback, please use the comments given below. You can also contact us through our[ support forums](https://www.syncfusion.com/forums), [Direct-Trac](https://www.syncfusion.com/support/directtrac/incidents), or [feedback portal](https://www.syncfusion.com/feedback). We are always happy to assist you!