I have a stored procedure that inserts into a table of invoice details and takes a json string as parameter in order to avoid a loop for each detail insert.
This is the stored procedure code:
CREATE PROCEDURE [dbo].[usp_Vta_ListInvoiceDetailCreate]
(@jsonDetailList NVARCHAR(MAX))
AS
BEGIN
SET NOCOUNT ON;
BEGIN
DECLARE @NUEVOS_DETALLES TABLE(ID BIGINT) --Tabla Variable
INSERT INTO CmpFacturasDetalle (FT_Id_Factura, PR_Id_Producto, AL_Id_Almacen,
MD_Id_Medida, FD_Cantidad, FD_Precio_Unitario,
FD_AlicuotaGR, FD_AlicuotaAdicional, FT_DescripcionProAmp,
FD_TotalIVAAplicado, FT_TotalDscto,
FD_TotalItem, FT_PorcentajeDscto,
FD_PartidaArancelaria, FD_SKU,
FD_CantXEmpaque, FD_Peso, FD_Nombre_Producto)
OUTPUT inserted.FD_Id_FacDetalle INTO @NUEVOS_DETALLES
SELECT
FT_Id_Factura, PR_Id_Producto, AL_Id_Almacen,
MD_Id_Medida, FD_Cantidad, FD_Precio_Unitario,
FD_AlicuotaGR, FD_AlicuotaAdicional, FT_DescripcionProAmp,
FD_TotalIVAAplicado, FT_TotalDscto,
FD_TotalItem, FT_PorcentajeDscto,
FD_PartidaArancelaria, FD_SKU,
FD_CantXEmpaque, FD_Peso, FD_Nombre_Producto
FROM
OPENJSON (@jsonDetailList)
WITH (
FT_Id_Factura BIGINT '$.FT_Id_Factura',
PR_Id_Producto BIGINT '$.PR_Id_Producto',
AL_Id_Almacen BIGINT '$.AL_Id_Almacen',
MD_Id_Medida BIGINT '$.idFaMD_Id_Medidactura',
FD_Cantidad DECIMAL(10,2) '$.FD_Cantidad',
FD_Precio_Unitario MONEY '$.FD_Precio_Unitario',
FD_AlicuotaGR DECIMAL(4,2) '$.FD_AlicuotaGR',
FD_AlicuotaAdicional DECIMAL(4,2) '$.FD_AlicuotaAdicional',
FT_DescripcionProAmp VARCHAR(3000) '$.FT_DescripcionProAmp',
FD_TotalIVAAplicado DECIMAL(18,4) '$.FD_TotalIVAAplicado',
FT_TotalDscto MONEY '$.FT_TotalDscto',
FD_TotalItem MONEY '$.FD_TotalItem',
FT_PorcentajeDscto MONEY '$.FT_PorcentajeDscto',
FD_PartidaArancelaria VARCHAR(50) '$.FD_PartidaArancelaria',
FD_SKU VARCHAR(50) '$.FD_SKU',
FD_CantXEmpaque DECIMAL(18,2) '$.FD_CantXEmpaque',
FD_Peso DECIMAL(18,2) '$.FD_Peso',
FD_Nombre_Producto VARCHAR(2500) '$.FD_Nombre_Producto'
)
SELECT ID FROM @NUEVOS_DETALLES;
END
END
When I add the stored procedure inside Entity Framework and try to call it, I get this error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_CmpFacturasDetalle_CmpFacturas". The conflict occurred in database "IERP", table "dbo.CmpFacturas", column 'FT_Id_Factura'.
This is how I call it:
// begin new transaction
db.Database.BeginTransaction();
// OutputParameter for invoiceHeader insert stored procedure
ObjectParameter FT_Id_Factura = new ObjectParameter("FT_Id_Factura", typeof(long));
// insert invoice header
var invoice_add = db.usp_Vta_InvoiceCreate(client.Name, Client.ID, FT_Id_Factura);
// Assign the invoice header ID to each detail
DetailList.ForEach(Det => Det.FT_Id_Factura = (long)FT_Id_Factura.Value);
var jsonList = JsonConvert.SerializeObject(factura.DetailList);
// insert invoice details without loop
var optimizedQuery = db.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.EnsureTransaction , @"EXEC [Operativo].[usp_Vta_ListInvoiceDetailCreate] @jsonDetailList = {0};", jsonList);
// commit current transaction
db.Database.CurrentTransaction.Commit();
If I manually set the 'FT_Id_Factura' foreign key to an existing invoice header the code works, but not the same with the row that isn't committed yet.
I'm using Visual Studio 2019 and Entity Framework 6.4.4 code-first in C#.
Thanks