How to Add Stored Procedures in ASP.NET Core Apps Using SPToCore
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (175).NET Core  (29).NET MAUI  (208)Angular  (109)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (41)Black Friday Deal  (1)Blazor  (220)BoldSign  (15)DocIO  (24)Essential JS 2  (107)Essential Studio  (200)File Formats  (67)Flutter  (133)JavaScript  (221)Microsoft  (119)PDF  (81)Python  (1)React  (101)Streamlit  (1)Succinctly series  (131)Syncfusion  (920)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (51)Windows Forms  (61)WinUI  (68)WPF  (159)Xamarin  (161)XlsIO  (37)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (151)Chart  (132)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (633)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (41)Extensions  (22)File Manager  (7)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (508)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (43)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (11)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (387)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (597)What's new  (333)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
How to Add Stored Procedures in ASP.NET Core Apps Using SPToCore

How to Add Stored Procedures in ASP.NET Core Apps Using SPToCore

Stored procedures 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:

Let’s get started!

Prerequisites

The following items should be installed in your machine.

Download the SPToCore tool

Download and extract the SPToCore tool from its GitHub location.

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 screenshot.
    Open the SPToCore tool using Visual Studio
  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
  3. In the code editor, enter your parameters as shown in the following screenshots.
    Enter your parameters in the code editor
    Parameter Details
    The following parameter details are reproduced from the SPToCore GitHub README.
    Parameter Details
  4.  After entering the parameters, run the SPToCore tool, and it will open the following window.
    Microsoft Visual Studio Debug Console Window
  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
  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

You can attach this automatically generated class to 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
  2. Create a folder and name it Entity.
    Create a folder and name it Entity
  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
  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
  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
  7. Override the OnConfiguring method by passing the following code in the automatically generated class file.
    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

  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
    sample stored procedure calling

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 80 components, our ASP.NET Core toolkit powered by Essential JS 2 contains all you need for building line-of-business applications, including popular widgets such as a DataGrid, Charts, Gantt Chart, Diagram, Spreadsheet, 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 page. If you are not yet a Syncfusion customer, you can try our 30-day free trial to check out the available features. Also, you can try our samples from this GitHub location.

If you wish to send us feedback, please use the comments given below. You can also contact us through our support forumssupport portal, or feedback portal. We are always happy to assist you!

Related blogs

Tags:

Share this post:

Comments (2)

Ghanshyam Tarsariya
Ghanshyam Tarsariya

Is this work for blazor webassembly project as well?

Yes, it will work for blazor webassembly project

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed