当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - LINQ throws exception in deployed code, not in IDE

问题描述:

My LINQ statement (query? variable? expression?) is throwing an exception when it's enumerated (.ToList() or foreach) [see below].

In most environments.

This error does not occur when I run from VisualStudio 2012 - it occurs when actually deployed to an environment.

There are a number of questions about this error on SO, but I would except those exceptions to be thrown all the time - not just in the IDE.

Yet.... the data is the same (there are test environments that point to the same databases that my machine uses).

The code is the same... just deployed.

If there was something fundamentally wrong with the LINQ, then the error should occur in all environments, right?

I''ve include some code, but I suspect it's an environmental problem, only I don't know what to look at in the environments.

When running in the IDE, it's using IIS Express, but IIS 7.0 (at least) is on the servers.

What are the likeliest trouble environmental trouble-spots? [Again, I don't think it's the code -- the code executes exception free in the IDE, with and without break-points.]

[I'm expecting I'll be editing this question, a bit.]


Some code.

Not sure of how much context is required.

Again - this code executes exactly as expected, with the same data, in the IDE.

Exceptions are thrown only when deployed.

// List<DbFirstEntityModelForms>

formsToImport = GetSelectedRecords(collection["keyField"]);

// List<DbFirstEntityModelForms>

baseForms = GetSelectedRecords(collection["keyField"]);

var importStates = context.DbFirstEntityModelStates.AsNoTracking()

.Where(l => context.DbFirstEntityModelForms.Where(f => f.CustomerCd == "IS" && f.LOBCd == baseLOB).Any(f => l.FormId == f.FormId && l.EditionDt == f.EditionDt));

//importStates = importStates.Where(l => formsToImport.Any(f => f.FormId == l.FormId && f.EditionDt == l.EditionDt));

// rewritten as below... still throws exception

foreach (var state in importStates)

{

// List<DbFirstEntityModelStates>

statesToImport.AddRange(formsToImport.Where(form => state.FormId == form.FormId && state.EditionDt == form.EditionDt).Select(form => state));

}


System.NotSupportedException: Unable to create a constant value of type 'DBFirstEntityObject'. Only primitive types or enumeration types are supported in this context.

at System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq)

at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()

at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.EnumerableValidator`3.Validate(IEnumerable`1 argument, String argumentName, Int32 expectedElementCount, Boolean allowEmpty, Func`3 map, Func`2 collect, Func`3 deriveName)

at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.EnumerableValidator`3.Validate()

at System.Data.Common.CommandTrees.ExpressionBuilder.Internal.ArgumentValidation.ValidateNewCollection(IEnumerable`1 elements, DbExpressionList& validElements)

at System.Data.Objects.ELinq.ExpressionConverter.NewArrayInitTranslator.TypedTranslate(ExpressionConverter parent, NewArrayExpression linq)

at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

at System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq)

at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)

at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)

at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)

at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)

at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)

at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)

at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)

at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)

at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)

at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)

at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

at System.Data.Objects.ELinq.ExpressionConverter.Convert()

at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)

at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)

at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()

网友答案:

Go to IIS and check the Application Pools and their .NET Versions. If you have installed v4.5 it might not be using it yet.

分享给朋友:
您可能感兴趣的文章:
随机阅读: