Introduction
In enterprise applications, especially in ERP / CRM, all business logic is often demanded to Stored Procedures.
Stored procedures are a powerful tool for developers whose know T-SQL to elaborate complex query or to implement core business logic with an eye on database performance.
Unluckily, in the new .NET Core 3, stored procedure support is not complete.
- You have to use FromSqlRaw ( or Query ) and compose your own SQL Command
- The resulting structure of the Stored Procedure must have an Entity Structure
- There is not a parameter mapping so each time you modify a stored procedure by adding a new parameter you have to manually fix the SQL Command
- There is not a scaffolding function that imports all the stored procedure
(Please refer to this article if you want to go deep on how to call stored procedure)
In today's article, I will show you how SPToCore utility can help you to manage a lot of stored procedures.
What is SPToCore ???
SPToCore is a utility that scaffolds all database stored procedures and creates a new dbContext that implements stored procedure methods, results, and parameter mappings.
You can find it on GitHub.
Step 1
First, create your existing SQL server database named "DB_WITH_SP" by executing the following SQL script:
- USE [DB_WITH_SP]
- GO
- /****** Object: Table [dbo].[anMaterial] Script Date: 12/05/2020 16:57:53 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[anMaterial](
- [idMaterial] [int] IDENTITY(1,1) NOT NULL,
- [Code] [varchar](50) NULL,
- [Material] [varchar](255) NULL,
- [Cost] [decimal](12, 2) NULL,
- [idMaterialType] [int] NULL,
- CONSTRAINT [PK_anMaterial] PRIMARY KEY CLUSTERED
- (
- [idMaterial] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- /****** Object: Table [dbo].[mdMaterialType] Script Date: 12/05/2020 16:57:53 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE TABLE [dbo].[mdMaterialType](
- [idMaterialType] [int] NOT NULL,
- [Code] [varchar](50) NULL,
- [MaterialType] [varchar](255) NULL,
- CONSTRAINT [PK_mdMaterialType] PRIMARY KEY CLUSTERED
- (
- [idMaterialType] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
- /****** Object: View [dbo].[vw_anMaterial] Script Date: 12/05/2020 16:57:53 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE VIEW [dbo].[vw_anMaterial]
- AS
- SELECT m.idMaterial,
- m.Code,
- m.Material,
- m.Cost,
- m.idMaterialType,
- MaterialTypeCode = t.Code,
- t.MaterialType
- FROM anMaterial m LEFT JOIN mdMaterialType t ON t.idMaterialType = m.idMaterialType
- GO
- SET IDENTITY_INSERT [dbo].[anMaterial] ON
- GO
- INSERT [dbo].[anMaterial] ([idMaterial], [Code], [Material], [Cost], [idMaterialType]) VALUES (1, N'MAT001', N'Cement', CAST(1000.00 AS Decimal(12, 2)), 1)
- GO
- INSERT [dbo].[anMaterial] ([idMaterial], [Code], [Material], [Cost], [idMaterialType]) VALUES (2, N'MAT002', N'Raw Meal', CAST(500.00 AS Decimal(12, 2)), 3)
- GO
- INSERT [dbo].[anMaterial] ([idMaterial], [Code], [Material], [Cost], [idMaterialType]) VALUES (3, N'MAT003', N'Clinker', CAST(750.00 AS Decimal(12, 2)), 2)
- GO
- INSERT [dbo].[anMaterial] ([idMaterial], [Code], [Material], [Cost], [idMaterialType]) VALUES (4, N'MAT004', N'Liquid Additive', CAST(50.00 AS Decimal(12, 2)), 3)
- GO
- INSERT [dbo].[anMaterial] ([idMaterial], [Code], [Material], [Cost], [idMaterialType]) VALUES (5, N'MAT005', N'Intermediate Binder', CAST(350.00 AS Decimal(12, 2)), 2)
- GO
- INSERT [dbo].[anMaterial] ([idMaterial], [Code], [Material], [Cost], [idMaterialType]) VALUES (6, N'MAT005', N'Clay', CAST(200.00 AS Decimal(12, 2)), 3)
- GO
- INSERT [dbo].[anMaterial] ([idMaterial], [Code], [Material], [Cost], [idMaterialType]) VALUES (7, N'MAT007', N'C4', CAST(100.00 AS Decimal(12, 2)), 3)
- GO
- SET IDENTITY_INSERT [dbo].[anMaterial] OFF
- GO
- INSERT [dbo].[mdMaterialType] ([idMaterialType], [Code], [MaterialType]) VALUES (1, N'FIN_PRO', N'Finish Product')
- GO
- INSERT [dbo].[mdMaterialType] ([idMaterialType], [Code], [MaterialType]) VALUES (2, N'INT_PRO', N'Intermediate Product')
- GO
- INSERT [dbo].[mdMaterialType] ([idMaterialType], [Code], [MaterialType]) VALUES (3, N'RAW_MAT', N'Raw Material')
- GO
- /****** Object: StoredProcedure [dbo].[Material_GetByCode] Script Date: 12/05/2020 16:57:53 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE PROC [dbo].[Material_GetByCode](
- @Code VARCHAR(50)
- )
- AS
- SELECT m.idMaterial,
- m.Code,
- m.Material,
- m.Cost,
- m.idMaterialType,
- m.MaterialTypeCode,
- m.MaterialType
- FROM vw_anMaterial m WHERE m.Code = @Code
- GO
- /****** Object: StoredProcedure [dbo].[Material_GetById] Script Date: 12/05/2020 16:57:53 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE PROC [dbo].[Material_GetById](
- @idMaterial INT
- )
- AS
- SELECT m.idMaterial,
- m.Code,
- m.Material,
- m.Cost,
- m.idMaterialType,
- m.MaterialTypeCode,
- m.MaterialType
- FROM vw_anMaterial m WHERE m.idMaterial = @idMaterial
- GO
- /****** Object: StoredProcedure [dbo].[Material_NEW] Script Date: 12/05/2020 16:57:53 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE PROC [dbo].[Material_NEW]
- (
- @Code VARCHAR(50),
- @Material VARCHAR(255),
- @Cost DECIMAL(12,2) NULL,
- @idMaterialType INT,
- @idMaterial INT OUT
- )
- AS
- INSERT INTO anMaterial
- (
- Code,
- Material,
- Cost,
- idMaterialType
- )
- VALUES ( @Code, @Material, @Cost, @idMaterialType )
- SET @idMaterial = @@IDENTITY
- GO
- /****** Object: StoredProcedure [dbo].[Material_SEARCH] Script Date: 12/05/2020 16:57:53 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE PROC [dbo].[Material_SEARCH]
- (
- @Code VARCHAR(50) NULL,
- @Material VARCHAR(255) NULL,
- @idMaterialType INT NULL
- )
- AS
- SELECT m.idMaterial,
- m.Code,
- m.Material,
- m.Cost,
- m.idMaterialType,
- m.MaterialTypeCode,
- m.MaterialType
- FROM vw_anMaterial m
- WHERE m.Code = ISNULL(@Code,m.Code)
- AND m.Material LIKE '%' + ISNULL(@Material,m.Material) + '%'
- AND m.idMaterialType = ISNULL(@idMaterialType, m.idMaterialType)
- GO
- /****** Object: StoredProcedure [dbo].[Material_UPD] Script Date: 12/05/2020 16:57:53 ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- CREATE PROC [dbo].[Material_UPD]
- (
- @idMaterial INT,
- @Material VARCHAR(255),
- @Cost DECIMAL(12,2) NULL,
- @idMaterialType INT NULL
- )
- AS
- UPDATE anMaterial
- SET Material = @Material,
- Cost = @Cost,
- idMaterialType = @idMaterialType
- WHERE idMaterial = @idMaterial
- GO







Paul JanssenPosted May 17, 2023, 2:38 PM
Do you have documentation on the options and arguments that are used with SPToCore?