dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.72k stars 3.17k forks source link

The variable name '@inserted1' has already been declared. Variable names must be unique within a query batch or stored procedure. #5468

Closed yarmenteros closed 2 years ago

yarmenteros commented 8 years ago

After upgrade EF Core rc1 to rc2 I am having problems saving several entities at the same time. The error is in the query generated by the EntityFramework as shown below:

Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateException' in Microsoft.EntityFrameworkCore.dll System.Data.SqlClient.SqlException: The variable name '@inserted1' has already been declared. Variable names must be unique within a query batch or stored procedure.

The generated batch:

exec sp_executesql N'SET NOCOUNT ON;
DECLARE @toInsert1 TABLE ([AdjustmentTypeKey] uniqueidentifier, [CompanyGroupShipKey] uniqueidentifier, [CreationDate] datetime2, [CreationUser] nvarchar(max), [DocumentDate] datetime2, [DocumentNo] bigint, [IsConfirmed] bit, [IsDeleted] bit, [LastModifiedDate] datetime2, [LastModifiedUser] nvarchar(max), [Notes] nvarchar(max), [PhysicalSequence] int, [Reference] nvarchar(max), [VoyageKey] uniqueidentifier, [_Position] [int]);
INSERT INTO @toInsert1
VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12, @p13, 0);

DECLARE @inserted1 TABLE ([AdjustmentHeadKey] uniqueidentifier, [AdjustmentTypeKey] uniqueidentifier, [CompanyGroupShipKey] uniqueidentifier, [CreationDate] datetime2, [CreationUser] nvarchar(max), [DocumentDate] datetime2, [DocumentNo] bigint, [IsConfirmed] bit, [IsDeleted] bit, [LastModifiedDate] datetime2, [LastModifiedUser] nvarchar(max), [Notes] nvarchar(max), [PhysicalSequence] int, [Reference] nvarchar(max), [RowVersion] binary(8), [VoyageKey] uniqueidentifier, [_Position] [int]);
MERGE [ICS].[AdjustmentHead] USING @toInsert1 AS i ON 1=0
WHEN NOT MATCHED THEN
INSERT ([AdjustmentTypeKey], [CompanyGroupShipKey], [CreationDate], [CreationUser], [DocumentDate], [DocumentNo], [IsConfirmed], [IsDeleted], [LastModifiedDate], [LastModifiedUser], [Notes], [PhysicalSequence], [Reference], [VoyageKey])
VALUES (i.[AdjustmentTypeKey], i.[CompanyGroupShipKey], i.[CreationDate], i.[CreationUser], i.[DocumentDate], i.[DocumentNo], i.[IsConfirmed], i.[IsDeleted], i.[LastModifiedDate], i.[LastModifiedUser], i.[Notes], i.[PhysicalSequence], i.[Reference], i.[VoyageKey])
OUTPUT INSERTED.[AdjustmentHeadKey], INSERTED.[AdjustmentTypeKey], INSERTED.[CompanyGroupShipKey], INSERTED.[CreationDate], INSERTED.[CreationUser], INSERTED.[DocumentDate], INSERTED.[DocumentNo], INSERTED.[IsConfirmed], INSERTED.[IsDeleted], INSERTED.[LastModifiedDate], INSERTED.[LastModifiedUser], INSERTED.[Notes], INSERTED.[PhysicalSequence], INSERTED.[Reference], INSERTED.[RowVersion], INSERTED.[VoyageKey], i._Position
INTO @inserted1;

SELECT [AdjustmentHeadKey], [RowVersion] FROM @inserted1
ORDER BY _Position;
DECLARE @inserted1 TABLE ([RowVersion] binary(8));
UPDATE [ICS].[Inventory] SET [LastModifiedDate] = @p15, [LastModifiedUser] = @p16, [PhysicalDate] = @p17, [QuantityOnHand] = @p18
OUTPUT INSERTED.[RowVersion]
INTO @inserted1
WHERE [InventoryKey] = @p14 AND [RowVersion] = @p19;
SELECT [RowVersion] FROM @inserted1;
DECLARE @inserted2 TABLE ([RowVersion] binary(8));
UPDATE [ICS].[Inventory] SET [LastModifiedDate] = @p21, [LastModifiedUser] = @p22, [PhysicalDate] = @p23, [QuantityOnHand] = @p24
OUTPUT INSERTED.[RowVersion]
INTO @inserted2
WHERE [InventoryKey] = @p20 AND [RowVersion] = @p25;
SELECT [RowVersion] FROM @inserted2;
DECLARE @inserted3 TABLE ([RowVersion] binary(8));
UPDATE [ICS].[Location] SET [LastModifiedDate] = @p27, [PhysicalSequence] = @p28
OUTPUT INSERTED.[RowVersion]
INTO @inserted3
WHERE [LocationKey] = @p26 AND [RowVersion] = @p29;
SELECT [RowVersion] FROM @inserted3;
DECLARE @inserted4 TABLE ([RowVersion] binary(8));
UPDATE [ICS].[PhysicalCountHead] SET [IsConfirmed] = @p31, [LastModifiedDate] = @p32, [PhysicalSequence] = @p33
OUTPUT INSERTED.[RowVersion]
INTO @inserted4
WHERE [PhysicalCountHeadKey] = @p30 AND [RowVersion] = @p34;
SELECT [RowVersion] FROM @inserted4;
',N'@p0 uniqueidentifier,@p1 uniqueidentifier,@p2 datetime2(7),@p3 nvarchar(4000),@p4 datetime2(7),@p5 bigint,@p6 bit,@p7 bit,@p8 datetime2(7),@p9 nvarchar(max) ,@p10 nvarchar(max) ,@p11 int,@p12 nvarchar(max) ,@p13 uniqueidentifier,@p14 uniqueidentifier,@p15 datetime2(7),@p16 nvarchar(4000),@p17 datetime2(7),@p18 decimal(3,3),@p19 varbinary(8),@p20 uniqueidentifier,@p21 datetime2(7),@p22 nvarchar(4000),@p23 datetime2(7),@p24 decimal(3,3),@p25 varbinary(8),@p26 uniqueidentifier,@p27 datetime2(7),@p28 int,@p29 varbinary(8),@p30 uniqueidentifier,@p31 bit,@p32 datetime2(7),@p33 int,@p34 varbinary(8)',@p0='A5EB3B62-7A1B-E611-80C2-9457A56B5217',@p1='E65EF798-89F6-E511-80C0-9457A56B5217',@p2='2016-05-23 11:02:48.1473911',@p3=N'yarmenteros',@p4='2016-05-20 00:00:00',@p5=3,@p6=1,@p7=0,@p8=NULL,@p9=NULL,@p10=NULL,@p11=2,@p12=NULL,@p13='36180824-FA1D-E611-80C2-9457A56B5217',@p14='0A26005B-A21E-E611-80C2-9457A56B5217',@p15='2016-05-23 11:02:48.1473911',@p16=N'yarmenteros',@p17='2016-05-20 00:00:00',@p18=0,@p19=0x000000000004AB52,@p20='0B26005B-A21E-E611-80C2-9457A56B5217',@p21='2016-05-23 11:02:48.1473911',@p22=N'yarmenteros',@p23='2016-05-20 00:00:00',@p24=0,@p25=0x0000000000048746,@p26='E5DA4888-061E-E611-80C2-9457A56B5217',@p27='2016-05-23 11:02:48.1473911',@p28=3,@p29=0x00000000000487C2,@p30='925DA1AE-A41E-E611-80C2-9457A56B5217',@p31=1,@p32='2016-05-23 11:02:48.1473911',@p33=2,@p34=0x00000000000487D3

I trying to save the follow entities:

AdjustmentHead (Add) - Relationship: WithMany AdjustmentDetail AdjustmentDetail (Add) - Relationship: WithOne AdjustmentHead

Location (Update) PhysicalCountHead (Update) - Relationship: WithMany PhysicalCountDetail, WithOne Location PhysicalCountDetail (Update) - Relationship: WithOne PhysicalCountHead

Inventory (Add and/or Update) - Relationship: WithOne Location

Thanks for your time...

rowanmiller commented 8 years ago

Duplicate of https://github.com/aspnet/EntityFramework/issues/5393 (already fixed in our working code base)