简体   繁体   English

为什么DbContext.SaveChangesAsync引发“子查询返回多个值”错误?

[英]Why is DbContext.SaveChangesAsync throwing a “Subquery returned more than 1 value” error?

The error in the title is occurring only in my QA environment right now, and not in my DEV environment. 标题中的错误目前仅在我的质量检查环境中发生,而在我的DEV环境中没有发生。 I've pointed my local machine towards the QA environment, ran a SQL trace, and can 1) reproduce the error and 2) see the actual SQL code being generated. 我已将本地计算机指向QA环境,运行了SQL跟踪,可以1)重现错误,2)查看正在生成的实际SQL代码。

What I don't understand is how in the hell an error like this is even possible on an update? 我不明白的是,到底怎么可能在更新中出现这样的错误? Here's my code: 这是我的代码:

            var investigation = await (
                        from  ti in DbContext.Investigation
                        where ti.Id == model.InvestigationId
                        select ti
                    ).SingleAsync();

            investigation.QueueId = model.QueueId;

            var result = await DbContext.SaveChangesAsync();

            return result;

I initially thought that first line (query) was returning more than one result, which would make sense. 最初,我认为第一行(查询)返回多个结果,这很有意义。 However, the error occurs on the var result = await DbContext.SaveChangesAsync(); 但是,该错误发生在var result = await DbContext.SaveChangesAsync(); line. 线。 Could someone please point me in the right direction to resolve this? 有人可以指出正确的方向来解决这个问题吗? My SQL query as picked-up by SQL Server Profiler: SQL Server Profiler提取的我的SQL查询:

exec sp_executesql N'SET NOCOUNT ON;
UPDATE [Investigation] SET [QueueId] = @p0
WHERE [Id] = @p1;
SELECT @@ROWCOUNT;

',N'@p1 bigint,@p0 smallint',@p1=7863,@p0=4

There is no subquery in this problem code as the error message suggests: 错误消息提示此问题代码中没有子查询:

exec sp_executesql N'SET NOCOUNT ON;
UPDATE [Investigation] SET [QueueId] = @p0
WHERE [Id] = @p1;
SELECT @@ROWCOUNT;
',N'@p1 bigint,@p0 smallint',@p1=7863,@p0=4

Consequently, the error must be in trigger code fired by the UPDATE statement. 因此,该错误必须在UPDATE语句触发的触发代码中。

I see from your comment that extraneous INSERT statements were inadvertently included in the trigger code. 我从您的评论中看到,无关的INSERT语句无意中包含在触发代码中。 A more common cause of these symptoms is that the trigger is improperly coded as to expect a single row to be affected. 这些症状的更常见原因是触发器的编码不正确,以至于期望单行受到影响。 A trigger fires once per statement, not per row, so it is important that trigger code handle the possibility of multiple rows. 触发器每个语句触发一次,而不是每行触发一次,因此触发代码处理多行的可能性很重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM