title: SqlServer 2005 Ok, MSLocalDb 2016 Error : LINQ to Entities only supports casting EDM primitive or enumeration types
body: <p>I have two server : production server SQL Server 2005 and my test server (LocalDb)\MSSQLLocalDB 2016</p>
  
  <p>I use .Net Framework 4.5.2 and EntityFramework 6, in a Linq request I do : </p>
  
  <pre><code>query.Select(l =&gt; l.s.Field1 + l.s.Field2).ToList();
  </code></pre>
  
  <ul>
  <li>Field1 : string </li>
  <li>Field2 : Nullable&lt; int ></li>
  </ul>
  
  <p>On the production server it works, the sql generated is : </p>
  
  <pre><code>SELECT CASE WHEN ([Extent1].[Field1] IS NULL) THEN N''
              ELSE [Extent1].[Field1] END +
         CASE WHEN ([Extent1].[Field2] IS NULL) THEN N''
              ELSE  CAST( [Extent1].[Field2] AS nvarchar(max))
         END AS [C1]
  </code></pre>
  
  <p>but on the local server I have this error : </p>
  
  <blockquote>
    <p>Unable to cast the type 'System.Nullable`1[[System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.</p>
  </blockquote>
  
  <p>I know how to solve my problem. </p>
  
  <ol>
  <li><p>I put my fields in a anonymous object, ToList() and I concatenate
  the fields after the ToList()</p></li>
  <li><p>another solution is :</p>
  
  <pre><code>query.Select(l =&gt; l.s.Field1 + SqlFunctions.StringConvert((decimal?)l.s.Field2).Trim())      
  </code></pre></li>
  </ol>
  
  <p>But I would like to understand why I have this difference : </p>
  
  <p>I have put the compatibiliy on my local server to 2008 same thing.</p>
  
  <p>If I do : </p>
  
  <ul>
  <li><p><code>Field2.Value</code> error : </p>
  
  <blockquote>
    <p>Unable to cast the type 'System.Int32' to type 'System.Object'. LINQ to Entities only supports casting EDM primitive or enumeration types.</p>
  </blockquote></li>
  <li><p><code>Field2.Value.ToString()</code> error : </p>
  
  <blockquote>
    <p>LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.**</p>
  </blockquote></li>
  </ul>
  
  <p><br></p>
  
  <ul>
  <li><p>This is a limitation of (LocalDb)\MSSQLLocalDB ? </p></li>
  <li><p>After 2005 this kind of thing is not allowed ? </p></li>
  <li><p>There is an option to activate/desactivate the implicit conversion
  ?</p></li>
  </ul>
  
  <p>Any clues are welcome.</p>