-4

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()
Michael Paulukonis
  • 9,020
  • 5
  • 48
  • 68
  • http://stackoverflow.com/questions/18929483/unable-to-create-a-constant-value-of-type-only-primitive-types-or-enumeration-ty – Gusman Feb 26 '16 at 19:57
  • I forgot to reference those existing questions. If that was the issue, I would expect the exception in the IDE, as well. – Michael Paulukonis Feb 26 '16 at 19:58
  • 3
    Post the code which causes the exception. – Gusman Feb 26 '16 at 19:59
  • As explained in the question, since the code does not throw an exception when running in the IDE, I believe this to be an Environmental issue, not code issue -- but have no idea what to look for as far as that is concerned. – Michael Paulukonis Feb 26 '16 at 20:01
  • 4
    But to see what happens in your environment we need to see the code, programmers still don't work with crystal balls... – Gusman Feb 26 '16 at 20:02
  • Do you have .NET Framework 4 on the server and .NET Framework 4.5 on your local box? – Pawel Feb 26 '16 at 20:05
  • 1
    Without code this question is unanswerable. You may be right about it being an environmental thing but without know what parts of the system/framework are being used, it's impossible to make an intelligent guess – Basic Feb 26 '16 at 20:08
  • @Pawel - I'll check that out. – Michael Paulukonis Feb 26 '16 at 20:10
  • @Bob - same database. I wondered the same thing, so it was the first thing I ruled out. When the code is successfully executed in my IDE, I can see the updates in the local test environment (same database). – Michael Paulukonis Feb 26 '16 at 20:11
  • Seems like a version mismatch problem. Either that or maybe you are connecting to a different db with a different schema? – Dabloons Feb 26 '16 at 20:30
  • It's 4.5 locally specified, and 4.5.1 installed on the server. – Michael Paulukonis Feb 26 '16 at 21:52

1 Answers1

0

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.

Application Pools

t3chb0t
  • 16,340
  • 13
  • 78
  • 118