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!
The following items should be installed in your machine.
Download and extract the SPToCore tool from its GitHub location.
After downloading the SPToCore tool, we need to get the stored procedure class file.
You can attach this automatically generated class to your .NET Core application by following the steps in the next section.
PM> Install-Package Microsoft.EntityFrameworkCore.SqlServer PM> Install-Package Microsoft.EntityFrameworkCore
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.
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.
Note: A sample stored procedure calling is shown below.
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
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 forums, support portal, or feedback portal. We are always happy to assist you!