gpt4 book ai didi

c# - EF 编译查询偶尔导致 SqlException

转载 作者:行者123 更新时间:2023-11-30 18:43:35 24 4
gpt4 key购买 nike

我正在使用编译查询,它在应用程序中执行我的部分查询逻辑非常复杂,它将编译查询抽象为 Func<>。我定期(大约一千次运行一次)收到以下异常:

SqlException:
The parameterized query '(@p__linq__0 int,@p__linq__1 nvarchar(4000),@p__linq__2 varchar(' expects the parameter '@p__linq__1', which was not supplied.

And InvalidOperationException:
Parameters cannot be added or removed from the parameter collection, and the parameter collection cannot be cleared after a query has been evaluated or its trace string has been retrieved.

看到第二个异常后,我的第一 react 是遇到了并发问题。这是在服务行为的默认配置下使用 basicHttpBinding 从 WCF 服务完成的,这意味着并发模式是单一的。我没有单独使用 InstanceContextMode,而是使用默认的 PerSession,这是否会导致问题?

所涉及的数据库被大量使用,所以我也怀疑我可能在查询期间超时,但我无法得出与我所看到的异常之间的逻辑关联。

雪上加霜的是,我无法在本地机器上重现这些错误,这些异常是在晚上远程服务器上的处理例程中发生的,而且每晚只发生一两次。希望其他人也经历过类似的事情。我可以尝试一些事情,例如更改 instancecontextmode、增加 SQL Server 超时,但我更愿意在尝试解决方法之前知道问题出在哪里。我确实记录了几个异常实例,这是其中一个 InvalidOperationExceptions 的堆栈帧:

System.Data.Objects.ObjectParameterCollection.Add(ObjectParameter parameter)
System.Data.Objects.ELinq.CompiledELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable&lt;T&gt;.GetEnumerator()
System.Linq.Enumerable.Count[TSource](IEnumerable`1 source, Func`2 predicate)
Processor.Processor.&lt;&gt;c__DisplayClass10.&lt;GetObjectContexts&gt;b__9(ObjectContextMetadata q) in C:\Documents and Settings\eugarps\My Documents\SomeService-development\Processor\Processor.cs:line 174
System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
System.Linq.Enumerable.&lt;DistinctIterator&gt;d__81`1.MoveNext()
System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value, IEqualityComparer`1 comparer)
Processor.Processor.&lt;&gt;c__DisplayClass10.&lt;GetObjectContexts&gt;b__c(ObjectContextMetadata q) in C:\Documents and Settings\eugarps\My Documents\SomeService-development\Processor\Processor.cs:line 179
System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
Processor.Processor.ResolveGeoCode(AddressItem address, String product) in C:\Documents and Settings\eugarps\My Documents\SomeService-development\Processor\Processor.cs:line 273
Company.ProcessingLogic.SomeService.SomeServiceService.ResolveDepotWithMask(AddressItem address, String product, DeliveryMap map, IGeocoder geocoder) in C:\Documents and Settings\eugarps\My Documents\SomeService-development\SomeServiceService\SomeServiceService.cs:line 136
Company.ProcessingLogic.SomeService.SomeServiceService.ResolveDepotWithMask(AddressItem address, String product, DeliveryMap map) in C:\Documents and Settings\eugarps\My Documents\SomeService-development\SomeServiceService\SomeServiceService.cs:line 59
Company.ProcessingLogic.SomeService.SomeServiceService.ResolveDepot(AddressItem address, String product) in C:\Documents and Settings\eugarps\My Documents\SomeService-development\SomeServiceService\SomeServiceService.cs:line 53
SyncInvokeResolveDepot(Object , Object[] , Object[] )
System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)
System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)
System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)

最佳答案

这可能与您发送的数据有关。请检查您发送的是否为空。

编辑

它也可能是竞争条件。 2 个线程是否可以使用同一个对象同时构建查询。如果是这样,您可能会遇到相同参数被添加两次的情况。

关于c# - EF 编译查询偶尔导致 SqlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4218889/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com