Skip to content

EF Core 6 reports variable name '@XXX' has already been declared #26754

Closed as duplicate

Description

@cecbr

Bug

I upgraded my project from NET5 to NET6, including EF CORE version 6.0.0.
I dont't have done any changes in code.
In an especific transaction, EF CORE 6 reports an error. When I downgraded to EF CORE 5.0.12, no errors occurred.

Console reported error:

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): The variable name '@inserted0' has already been declared. Variable names must be unique within a query batch or stored procedure.

EF debug query

    SET NOCOUNT ON;
      DECLARE @inserted0 TABLE ([HistoryId] uniqueidentifier);
      INSERT INTO [AwardHistory] ([AwardType], [BadgeId], [CreationDate], [Points], [PublicationId], [TenantId], [TopicId], [UserId])
      OUTPUT INSERTED.[HistoryId]
      INTO @inserted0
      VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7);
      SELECT [t].[HistoryId], [t].[CreationYearMonth], [t].[CreationYearWeek] FROM [AwardHistory] t
      INNER JOIN @inserted0 i ON ([t].[HistoryId] = [i].[HistoryId]);

      UPDATE [AwardRank] SET [Points] = @p8
      WHERE [TenantId] = @p9 AND [UserId] = @p10 AND [YearMonth] = @p11;
      SELECT @@ROWCOUNT;

      UPDATE [Notification] SET [CreationDate] = @p12
      WHERE [NotificationId] = @p13;
      SELECT @@ROWCOUNT;

      UPDATE [Notification] SET [CreationDate] = @p14
      WHERE [NotificationId] = @p15;
      SELECT @@ROWCOUNT;

      UPDATE [Notification] SET [CreationDate] = @p16
      WHERE [NotificationId] = @p17;
      SELECT @@ROWCOUNT;

      UPDATE [Notification] SET [CreationDate] = @p18
      WHERE [NotificationId] = @p19;
      SELECT @@ROWCOUNT;

      UPDATE [Notification] SET [CreationDate] = @p20
      WHERE [NotificationId] = @p21;
      SELECT @@ROWCOUNT;

      UPDATE [Notification] SET [CreationDate] = @p22
      WHERE [NotificationId] = @p23;
      SELECT @@ROWCOUNT;

      UPDATE [Notification] SET [CreationDate] = @p24
      WHERE [NotificationId] = @p25;
      SELECT @@ROWCOUNT;

      UPDATE [Notification] SET [CreationDate] = @p26
      WHERE [NotificationId] = @p27;
      SELECT @@ROWCOUNT;

      UPDATE [Notification] SET [CreationDate] = @p28
      WHERE [NotificationId] = @p29;
      SELECT @@ROWCOUNT;

      INSERT INTO [Notification] ([NotificationId], [CreationDate], [Day], [Deleted], [EmailSentOn], [GroupId], [Hide], [NotificationData], [NotificationSendOption], [NotificationType], [RelatedActionId], [RelatedAnswerId], [RelatedCommentId], [RelatedOldObjectName], [RelatedTopicId], [RelatedUserId], [TargetPublicationId], [UserId])
      VALUES (@p30, @p31, @p32, @p33, @p34, @p35, @p36, @p37, @p38, @p39, @p40, @p41, @p42, @p43, @p44, @p45, @p46, @p47),
      (@p48, @p49, @p50, @p51, @p52, @p53, @p54, @p55, @p56, @p57, @p58, @p59, @p60, @p61, @p62, @p63, @p64, @p65),
      (@p66, @p67, @p68, @p69, @p70, @p71, @p72, @p73, @p74, @p75, @p76, @p77, @p78, @p79, @p80, @p81, @p82, @p83),
      (@p84, @p85, @p86, @p87, @p88, @p89, @p90, @p91, @p92, @p93, @p94, @p95, @p96, @p97, @p98, @p99, @p100, @p101),
      (@p102, @p103, @p104, @p105, @p106, @p107, @p108, @p109, @p110, @p111, @p112, @p113, @p114, @p115, @p116, @p117, @p118, @p119);
      UPDATE [Publication] SET [ApprovalStatus] = @p120, [FirstApprovalDate] = @p121, [IndexRequestDate] = @p122, [IndexRequestType] = @p123, [LastApprovalDate] = @p124, [PublishDate] = @p125
      WHERE [PublicationId] = @p126 AND [RowVersion] = @p127;
      SELECT [Deleted], [FirstApprovalYearMonth], [FirstApprovalYearWeek], [IsExclusiveCourseItem], [RowVersion], [Score], [ViewFirstHighlightYearMonth], [ViewFirstHighlightYearWeek]
      FROM [Publication]
      WHERE @@ROWCOUNT = 1 AND [PublicationId] = @p126;

      DECLARE @inserted17 TABLE ([Id] bigint, [_Position] [int]);
      MERGE [PublicationLog] USING (
      VALUES (@p128, @p129, @p130, @p131, 0),
      (@p132, @p133, @p134, @p135, 1)) AS i ([CreationDate], [CreatorId], [JsonData], [PublicationId], _Position) ON 1=0
      WHEN NOT MATCHED THEN
      INSERT ([CreationDate], [CreatorId], [JsonData], [PublicationId])
      VALUES (i.[CreationDate], i.[CreatorId], i.[JsonData], i.[PublicationId])
      OUTPUT INSERTED.[Id], i._Position
      INTO @inserted17;

      SELECT [i].[Id] FROM @inserted17 i
      ORDER BY [i].[_Position];

      DECLARE @inserted0 TABLE ([Id] uniqueidentifier);
      INSERT INTO [SmtpQueue] ([Body], [BodyInHtml], [LastProcessingDate], [LastStatusCode], [PostedOn], [RecipientEmail], [RecipientId], [RecipientName], [Retries], [SenderEmail], [SenderName], [Subject], [TenantId])
      OUTPUT INSERTED.[Id]
      INTO @inserted0
      VALUES (@p136, @p137, @p138, @p139, @p140, @p141, @p142, @p143, @p144, @p145, @p146, @p147, @p148);
      SELECT [t].[Id], [t].[LastStatus] FROM [SmtpQueue] t
      INNER JOIN @inserted0 i ON ([t].[Id] = [i].[Id]);

      UPDATE [UserPassport] SET [AwardPoints] = @p149
      WHERE [UserId] = @p150 AND [RowVersion] = @p151;
      SELECT [CombinedRoles], [Deleted], [IsActive], [IsManager], [RowVersion], [ThumbnailUrl]
      FROM [UserPassport]
      WHERE @@ROWCOUNT = 1 AND [UserId] = @p150;

      UPDATE [UserStatistics] SET [Posts] = @p152
      WHERE [UserId] = @p153;
      SELECT [LikesBalance], [LikesReceivedBalance], [TotalPublications]
      FROM [UserStatistics]
      WHERE @@ROWCOUNT = 1 AND [UserId] = @p153;

EF Core version: 6.0.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions