Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (143).NET Core  (23)Angular  (39)ASP.NET  (47)ASP.NET Core  (53)ASP.NET MVC  (83)Azure  (26)Blazor  (47)DocIO  (18)Essential JS 2  (64)Essential Studio  (159)Flutter  (52)JavaScript  (134)Microsoft  (96)PDF  (48)React  (29)Succinctly series  (118)Syncfusion  (399)TypeScript  (30)Uno Platform  (2)UWP  (4)Vue  (25)Webinar  (14)Windows Forms  (52)WPF  (92)Xamarin  (105)XlsIO  (20)Other CategoriesBarcode  (4)BI  (29)Bold BI  (3)Build conference  (6)Business intelligence  (53)Button  (4)C#  (99)Chart  (40)Cloud  (8)Company  (445)Dashboard  (4)Data Science  (3)Data Validation  (2)DataGrid  (31)Development  (233)Doc  (7)eBook  (89)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (13)Extensions  (7)File Manager  (3)Gantt  (6)Gauge  (4)Git  (3)Grid  (24)HTML  (8)Installer  (1)Knockout  (2)LINQPad  (1)Linux  (1)M-Commerce  (1)Metro Studio  (11)Mobile  (139)Mobile MVC  (9)OLAP server  (1)Orubase  (12)Partners  (20)PDF viewer  (15)Performance  (2)PHP  (1)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (5)Road Map  (9)Scheduler  (15)SfDataGrid  (7)Silverlight  (21)Sneak Peek  (12)Solution Services  (2)Spreadsheet  (2)SQL  (4)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (9)Tips and Tricks  (15)UI  (62)Uncategorized  (68)Unix  (2)User interface  (48)Visual State Manager  (1)Visual Studio  (12)Visual Studio Code  (7)Web  (96)What's new  (61)Windows 8  (19)Windows App  (1)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Share on twitter
Share on facebook
Share on linkedin
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 the 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

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
  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 70 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 forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Tags:

Share this post:

Share on twitter
Share on facebook
Share on linkedin

Comments (2)

Is this work for blazor webassembly project as well?

Yes, it will work for blazor webassembly project

Leave a comment

Popular Now

Be the first to get updates

Subscribe RSS feed
Scroll To Top