Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.InvalidOperationException:从物化的';System.Int32';键入可为空的';国家';类型无效_C#_.net_Linq_Entity Framework - Fatal编程技术网

C# System.InvalidOperationException:从物化的';System.Int32';键入可为空的';国家';类型无效

C# System.InvalidOperationException:从物化的';System.Int32';键入可为空的';国家';类型无效,c#,.net,linq,entity-framework,C#,.net,Linq,Entity Framework,我有一个特定的单元测试,它可以在我的个人电脑上正常运行,但每当我让TFS运行测试时,它都会失败,出现以下异常- System.InvalidOperationException:从 不允许将“System.Int32”类型物化为可为空的“Country”类型 有效 通过跟踪堆栈跟踪,以下方法存在问题- public IEnumerable<IAddress> AddressSelectAll(long userID) { using (var context = new En

我有一个特定的单元测试,它可以在我的个人电脑上正常运行,但每当我让TFS运行测试时,它都会失败,出现以下异常-

System.InvalidOperationException:从 不允许将“System.Int32”类型物化为可为空的“Country”类型 有效

通过跟踪堆栈跟踪,以下方法存在问题-

public IEnumerable<IAddress> AddressSelectAll(long userID)
{
    using (var context = new Entities())
    {
        var addresses = context.Customers
                               .Where(x => x.UserId == userID)
                               .Select(y => 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();
    }
}
我的edmx有以下内容-

<Function Name="ConvertToInt32" ReturnType="Edm.Int32">
  <Parameter Name="v" Type="Edm.String" />
  <DefiningExpression>
    CAST(v AS Edm.Int32)
  </DefiningExpression>
</Function>

铸造(v为Edm.Int32)

有人能告诉我我做错了什么吗?

你的问题可能是线路(或线路的一部分)有问题

你在一个案例和一个国家中为国家设定了价值?另一方面。也许您可以将该值替换为-1,或者更可靠地将Customer.Country类型更改为Country?而不是国家

<Function Name="ConvertToInt32" ReturnType="Edm.Int32">
  <Parameter Name="v" Type="Edm.String" />
  <DefiningExpression>
    CAST(v AS Edm.Int32)
  </DefiningExpression>
</Function>
Country = y.Country != null ? (Country)y.Country : (Country?)null