调用Any或Count后,Entity Framework会抛出NotSupportedException

时间:2014-01-30 08:45:06

标签: c# sql entity-framework

我有以下代码:

private void Import(DbSet<DBEntity> dbEntities, IEnumerable<ExcelEntity> entities, ClapEntities context)
{
    foreach (var me in entities)
    {
        try
        {
            var dbe = dbEntities.Where(IsEqualRecord(me, context));
            bool hasElement = dbe.Any(); //Line which throws the exception
        }
        catch (Exception ex)
        {
            //...
        }
    }
}

protected abstract Expression<Func<DBEntity, bool>> IsEqualRecord(ExcelEntity entity, ClapEntities context);

Any()扩展名会引发NotSupportedException

  

System.Exception:无法将实体映射到数据库。映射器:[CuttingToolImport]; Rownumber:[153] ---&gt; System.NotSupportedException:方法“First”只能用作最终查询操作。请考虑在此实例中使用方法'FirstOrDefault'   在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.FirstTranslator.TranslateUnary(ExpressionConverter parent,DbExpression操作数,MethodCallExpression调用)
  at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent,MethodCallExpression linq)
  在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
  在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter parent,BinaryExpression linq)
  在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.BinaryTranslator.TypedTranslate(ExpressionConverter parent,BinaryExpression linq)
  在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
  在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda,DbExpression输入)
  在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent,MethodCallExpression call,DbExpression&amp; source,DbExpressionBinding&amp; sourceBinding,DbExpression&amp; lambda)
  在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent,MethodCallExpression call)
  at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent,MethodCallExpression linq)
  在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
  在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.AggregateTranslator.Translate(ExpressionConverter parent,MethodCallExpression call)
  at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent,MethodCallExpression linq)
  在System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert()
  at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable 1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery
1.&lt;&gt; c_ DisplayClassb.b _a()
  at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction [T](Func 1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery
1.&lt;&gt; c_ DisplayClassb.b _9()
  at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute [TResult](Func 1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery
1.GetResults(Nullable 1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery
1..GetEnumerator&gt; b__0()
  在System.Lazy 1.CreateValue()
at System.Lazy
1.LazyInitValue()
  在System.Data.Entity.Internal.LazyEnumerator 1.MoveNext()
at System.Linq.Enumerable.Single[TSource](IEnumerable
1来源)
  在System.Linq.Queryable.Count [TSource](IQueryable 1 source)
at CLAP.Models.Import.ImportMapping.ImportBase
2.Import(DbSet 1 dbEntities, IEnumerable 1个实体,ClapEntities上下文)
  ---内部异常堆栈跟踪结束---

正如您所看到的,Any()扩展程序(或Count()扩展名)使用了First()内部扩展名,这是不允许的。我不知道如何解决这个错误。

这个代码确实在Entity Framework第5版或第6版和

中工作了好几个月
Microsoft SQL Server 2012 - 11.0.2100.60 (X64) Express Edition (64-bit) on
Windows NT 6.1 <X64> (Build 7601: Service Pack 1)

有没有人知道如何解决这个错误?

1 个答案:

答案 0 :(得分:5)

您应该使用.FirstOrDefault()代替当数据集中没有实体时会返回NULL

bool hasElement = dbe.FirstOrDefault() != null;

代码中的另一个可能的问题是您使用的自定义IsEqualRecord方法应包含代码,该代码可以转换为 SQL