2
我有一个特殊的单元测试,可以在我的个人电脑上正常运行,但每当我运行TFS运行测试时,出现以下异常 -System.InvalidOperationException:从物化“System.Int32”类型指定为可空的“国家”类型无效
System.InvalidOperationException:从 指定的强制转换物化“System.Int32”类型为可空“国家”类型不是 有效。
通过下面的堆栈跟踪,它与下面的方法有问题 -
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();
}
}
在情况下,它相关的(怀疑是),我的EFFunctions类定义为 -
public static class EFFunctions
{
[EdmFunction("Model", "ConvertToInt32")]
public static int ConvertToInt32(string text)
{
var result = string.IsNullOrEmpty(text) ? 0 : Convert.ToInt32(text);
return result;
}
}
而我的.edmx有以下内容 -
<Function Name="ConvertToInt32" ReturnType="Edm.Int32">
<Parameter Name="v" Type="Edm.String" />
<DefiningExpression>
CAST(v AS Edm.Int32)
</DefiningExpression>
</Function>
有人能告诉我我做错了什么吗?