2012-02-29 150 views
0

在我的项目中,我使用实体框架与MySQL。 此代码:实体框架:值不能为空。参数名称:key

objectContext.users.Include("posts").Take(2).ToList() 

抛出异常:System.ArgumentNullException: Value cannot be null. Parameter name: key

这样的堆栈跟踪:

[ArgumentNullException: Value cannot be null. 
Parameter name: key] 
    System.Collections.Generic.Dictionary`2.FindEntry(TKey key) +12670485 
    MySql.Data.Entity.Scope.GetFragment(String name) +27 
    MySql.Data.Entity.SelectStatement.AddDefaultColumns(Scope scope) +177 
    MySql.Data.Entity.SelectStatement.Wrap(Scope scope) +90 
    MySql.Data.Entity.SelectGenerator.WrapJoinInputIfNecessary(InputFragment fragment, Boolean isRightPart) +251 
    MySql.Data.Entity.SelectGenerator.HandleJoinExpression(DbExpressionBinding left, DbExpressionBinding right, DbExpressionKind joinType, DbExpression joinCondition) +110 
    MySql.Data.Entity.SelectGenerator.Visit(DbJoinExpression expression) +33 
    MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +50 
    MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +19 
    MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression) +45 
    MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +50 
    MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +19 
    MySql.Data.Entity.SelectGenerator.Visit(DbSortExpression expression) +61 
    MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +50 
    MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +19 
    MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression) +45 
    MySql.Data.Entity.SelectGenerator.GenerateSQL(DbCommandTree tree) +73 
    MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree) +401 
    System.Data.EntityClient.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory, DbCommandTree commandTree) +608 

有趣的是,objectContext.users.Take(2).ToList()objectContext.users.Include("posts").ToList()工作正常。

任何人都遇到过这个问题?

回答

3

是的。完全相同的问题。我确认take存在join似乎是问题所在。在我的情况下,因为我将EF查询投影到一个对象上,直接在数据库上执行take,而不是在投影之后进行本地(等效于objectContext.users.Include("posts").ToList()),这会在耗用时间和内存消耗方面产生巨大差异。

相关问题