C# 如何解决多次调用存储过程时引发的EF 6超时过期异常?
在插入操作期间,我正在对SQL server数据库运行存储过程。当我尝试在循环中插入多行时,在随后执行存储过程时会出现以下错误:C# 如何解决多次调用存储过程时引发的EF 6超时过期异常?,c#,sql-server,entity-framework,stored-procedures,C#,Sql Server,Entity Framework,Stored Procedures,在插入操作期间,我正在对SQL server数据库运行存储过程。当我尝试在循环中插入多行时,在随后执行存储过程时会出现以下错误: System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.Sql
System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception: The wait operation timed out
在一次尝试中,没有抛出异常,但在列中插入了null值,而它应该是存储过程生成的整数
以下是我的存储过程:
ALTER procedure [dbo].[CreateOrRetrieveDataScope] (@dataScopeTypeId int ,@clientId nvarchar(8) ,@areaId int = NULL, @unitId bigint = NULL ,@wheelPosition int = NULL) with recompile as
declare @DataScopeId int
Select_DataScopeId:
set @DataScopeId = (select Id from DataScopes
where DataScopeTypeId = @dataScopeTypeId
and ClientId = @clientId
and (AreaId is null or (AreaId = @areaId))
and (UnitId is null or (UnitId = @unitId))
and (WheelPosition is null or (WheelPosition = @wheelPosition)))
if @DataScopeId is null begin
begin try
insert into DataScopes(DataScopeTypeId, ClientId, AreaId, UnitId, WheelPosition) values (@dataScopeTypeId, @clientId, @areaId, @unitId, @wheelPosition);
goto Select_DataScopeId
end try
begin catch
goto Select_DataScopeId
end catch
end
select @DataScopeId
基本上,这里我们尝试创建一个数据范围id值,如果它不存在的话。如果它已经存在,我们就从proc返回它。我测试了两个几乎同时插入的代码,以确保不会有重复。如果插入了值,则第二个客户端应始终接收该值
下面是我的windows服务中执行它的代码行:
newJob.DataScopeId = db.CreateOrRetrieveDataScope(1, areaMap.Area.ClientID, areaMap.AreaID, null, null).FirstOrDefault().Value;
存储的过程针对数据库中一个表中的多行执行。基本上,新作业是从另一个表中的行创建的
到目前为止,我已经尝试在活动监视器执行操作时观察它,但我无法在那里收集到任何异常情况。我还尝试在SQLServerManagementStudio中将连接数设置为0无限制
编辑:再看一下活动监视器,我的存储过程似乎已挂起。如果我正确地记住了时间跨度,则异常意味着您的进程需要30秒以上才能执行。因此,也许我应该尝试暂时增加数据库上下文的超时时间?如果有任何区别,则select是活动监视器中挂起状态旁边的命令。我知道如何在sql命令上设置它,但不知道如何在EFA中更改它。在黑暗中作为旁注/快照,如果插入失败,包装INSERT命令的CATCH块将触发无限循环。catch会将存储过程发送到开始,它会再次尝试插入,再次失败,将它发送回开始,尝试插入,失败,返回开始…另一方面,我会调试该过程,如果你达到超时,它会运行太长,不管它做什么