有时,并非总是如此,我遇到了以下错误:"底层提供程序在打开时失败了."
这是我的情况:
我有一个并行处理的整数键列表,用作编译的选择查询中的参数.我在RIA域服务中使用它.
var queryResult = new List<int> {1, 2, 3}.AsParallel().Select(i => CompiledQueries.GetRecordByKey(this.ObjectContext, i)).ToList();
Run Code Online (Sandbox Code Playgroud)
这是编译的查询的样子:
public static IEnumerable<CompiledQueryResult> GetRecordByKey(MyEntities _context, int _key)
{
if (_getRecordByKey == null)
{
_getRecordByKey = CompiledQuery.Compile<MyEntities, int, IEnumerable<CompiledQueryResult>>
((ctx, key) =>
ctx.Records
.Where(r => r.Id == key)
.Select(r => new CompiledQueryResult
{
Id = r.ID,
Name = r.Name,
...
})
);
}
return _getRecordByKey.Invoke(_context, _key);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用EF4,RIA(实际上domainservice的ObjectContext被传递给编译的查询方法),连接字符串包含着名的MultipleActiveResultSets = True ...当MultipleActiveResultSets设置为false时,我立即得到错误.
这里使用的代码是真实代码的简化版本.我也传递了更多的键,因此更多的并行查询..有时我在内部异常中看到数据读取器正在关闭,但状态是连接..
我试图扩大连接池大小,但没有成功.
有没有好的建议来解决这个问题?Thx提前.
我认为编译的查询将执行与DataContext相同的查询转换.然而,当我尝试使用带有.Contains方法调用的查询时,我遇到了运行时错误.我哪里出错了?
//private member which holds a compiled query.
Func<DataAccess.DataClasses1DataContext, List<int>, List<DataAccess.TestRecord>>
compiledFiftyRecordQuery = System.Data.Linq.CompiledQuery.Compile
<DataAccess.DataClasses1DataContext, List<int>, List<DataAccess.TestRecord>>
((dc, ids) => dc.TestRecords.Where(tr => ids.Contains(tr.ID)).ToList());
//this method calls the compiled query.
public void FiftyRecordCompiledQueryByID()
{
List<int> IDs = GetRandomInts(50);
//System.NotSupportedException
//{"Parameters cannot be sequences."}
List<DataAccess.TestRecord> results = compiledFiftyRecordQuery
(myContext, IDs);
}
Run Code Online (Sandbox Code Playgroud) 我的项目(UI层是asp.mvc)是使用.NET 3.5开发的.升级到.NET 4.0后,我遇到了编译查询的问题:
[ArgumentException: Query was compiled for a different mapping source than the one associated with the specified DataContext.]
System.Data.Linq.CompiledQuery.ExecuteQuery(DataContext context, Object[] args) +863348
System.Data.Linq.CompiledQuery.Invoke(TArg0 arg0, TArg1 arg1) +110
Run Code Online (Sandbox Code Playgroud)
每当我运行我的查询时,我都会传递我的上下文
return StaticQueries.getTopFiveOrders(mContext, int howMany);
public static Func<Mycontext, int, IQueryable<Order>> getTopFiveOrders
= CompiledQuery.Compile
((Mycontext mContext, int howMany) =>
( some query).Distinct());
Run Code Online (Sandbox Code Playgroud)
第二个请求发生错误.