我有以下代码:
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: Could not map entity to database. Mapper: [CuttingToolImport]; Rownumber: [153] ---> System.NotSupportedException: The method 'First' can only be used as a final query operation. Consider using the method 'FirstOrDefault' in this instance instead.
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.FirstTranslator.TranslateUnary(ExpressionConverter parent, DbExpression operand, MethodCallExpression call)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.EqualsTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.BinaryTranslator.TypedTranslate(ExpressionConverter parent, BinaryExpression linq)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)
at 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)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)
at 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)
at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.Convert()
at System.Data.Entity.Core.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable1 forMergeOption)<br/> at System.Data.Entity.Core.Objects.ObjectQuery
1.<>c_DisplayClassb.b_a()
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)<br/> at System.Data.Entity.Core.Objects.ObjectQuery
1.<>c_DisplayClassb.b_9()
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation)<br/> at System.Data.Entity.Core.Objects.ObjectQuery
1.GetResults(Nullable1 forMergeOption)<br/> at System.Data.Entity.Core.Objects.ObjectQuery
1..GetEnumerator>b__0()
at System.Lazy1.CreateValue()<br/> at System.Lazy
1.LazyInitValue()
at System.Data.Entity.Internal.LazyEnumerator1.MoveNext()<br/> at System.Linq.Enumerable.Single[TSource](IEnumerable
1 source)
at System.Linq.Queryable.Count[TSource](IQueryable1 source)<br/> at CLAP.Models.Import.ImportMapping.ImportBase
2.Import(DbSet1 dbEntities, IEnumerable
1 entities, ClapEntities context)
--- End of inner exception stack trace ---
如您所见,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)
有没有人知道如何解决该错误?
最佳答案
您应该改用 .FirstOrDefault()
,当数据集中没有实体时,它将返回 NULL
:
bool hasElement = dbe.FirstOrDefault() != null;
代码中的另一个可能问题是您正在使用自定义IsEqualRecord
方法,该方法应该只 可以翻译成< em>SQL。
关于c# - Entity Framework 在调用 Any 或 Count 后抛出 NotSupportedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21451520/