C# System.InvalidOperationException:从物化的';System.Int32';键入可为空的';国家';类型无效
我有一个特定的单元测试,它可以在我的个人电脑上正常运行,但每当我让TFS运行测试时,它都会失败,出现以下异常- System.InvalidOperationException:从 不允许将“System.Int32”类型物化为可为空的“Country”类型 有效 通过跟踪堆栈跟踪,以下方法存在问题-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
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