IQueryable .ToList方法抛出KeyNotFoundException

时间:2017-12-13 02:02:02

标签: c# .net entity-framework .net-core iqueryable

我正在使用.NET Core 2.0和Entity Framework。在此代码的最后一行:

IQueryable<MySpecialObject> specialObjects = _context.MySpecialObject.Where(ss => ss.IsActive == Constants.ISACTIVEFLAG)
                                                              .Join(_context.SampleRecord,
                                                                    ss => ss.SampleRecordId,
                                                                    sr => sr.SampleRecordId,
                                                                    (ss, sr) => new { ss, ownerId = sr.CustomerId })
                                                              .Select(comb => new MySpecialObject
                                                              {
                                                                  sampleShareId = comb.ss.SampleShareId,
                                                                  sampleRecordId = comb.ss.SampleRecordId,
                                                                  ownerName = namedCustomers[comb.ownerId],
                                                                  sharedWithName = namedCustomers[comb.ss.GuestCustomerId],
                                                                  createdDate = comb.ss.CreateDate
                                                              });

var listOfSpecialObjects = specialObjects.ToList();

我得到了这个例外:

2017-12-12 15:09:59.0643|ERROR|Microsoft.EntityFrameworkCore.Query|An exception occurred in the database while iterating the results of a query for context type 'Models.DataHubCoreContext'.
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.ThrowHelper.ThrowKeyNotFoundException()
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at lambda_method(Closure , QueryContext , TransparentIdentifier`2 )
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ProjectionShaper.TypedProjectionShaper`3.Shape(QueryContext queryContext, ValueBuffer valueBuffer)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.BufferlessMoveNext(Boolean buffer)
at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()

什么可能导致Entity Framwork抛出异常?在ToList()方法的文档中,它只说它可以抛出System.ArgumentNullException。我正在使用名为“namedCustomers”的词典,但在下一行代码之前不会抛出异常。

0 个答案:

没有答案
相关问题