title: System.InvalidOperationException: The specified cast from a materialized 'System.Int32' type to a nullable 'Country' type is not valid
body: <p>I have a particular unit test that runs fine on my personal PC, but whenever I get TFS to run the test, it fails with the following exception - </p>
  
  <blockquote>
    <p>System.InvalidOperationException: The specified cast from a
    materialized 'System.Int32' type to a nullable 'Country' type is not
    valid.</p>
  </blockquote>
  
  <p>By following the stack trace, it has a problem with the following method - </p>
  
  <pre><code>public IEnumerable&lt;IAddress&gt; AddressSelectAll(long userID)
  {
      using (var context = new Entities())
      {
          var addresses = context.Customers
                                 .Where(x =&gt; x.UserId == userID)
                                 .Select(y =&gt; new Address
                                                  {
                                                      Address1 = y.Address1,
                                                      Address2 = y.Address2,
                                                      Address3 = y.Address3,
                                                      AddressID = y.AddressId,
                                                      City = y.City,
                                                      Country = y.Country != null ? (Country)y.Country : (Country?)null,
                                                      Postcode = y.Postcode,
                                                      State = y.State,
                                                      RecordEntryDate = y.RecordEntryDate,
                                                      Type = (AddressType)EFFunctions.ConvertToInt32(y.AddressType),
                                                      UserID = y.UserId
                                                  }).ToList();
  
          return addresses.ToList();
      }
  }
  </code></pre>
  
  <p>In case it's relevant (doubt it is), my EFFunctions class is defined as - </p>
  
  <pre><code>public static class EFFunctions
  {
      [EdmFunction("Model", "ConvertToInt32")]
      public static int ConvertToInt32(string text)
      {
          var result = string.IsNullOrEmpty(text) ? 0 : Convert.ToInt32(text);
  
          return result;
      }
  }
  </code></pre>
  
  <p>And my .edmx has the following in it - </p>
  
  <pre><code>&lt;Function Name="ConvertToInt32" ReturnType="Edm.Int32"&gt;
    &lt;Parameter Name="v" Type="Edm.String" /&gt;
    &lt;DefiningExpression&gt;
      CAST(v AS Edm.Int32)
    &lt;/DefiningExpression&gt;
  &lt;/Function&gt;
  </code></pre>
  
  <p>Is anyone able to tell me what I'm doing wrong?</p>