![](/img/trans.png)
[英]The specified cast from a materialized 'System.Int32' type to the 'System.Int64' type is not valid
[英]System.InvalidOperationException: The specified cast from a materialized 'System.Int32' type to a nullable 'Country' type is not valid
我有一个可以在个人PC上正常运行的特定单元测试,但是每当我让TFS运行测试时,它都会失败,并出现以下异常-
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>
有人能告诉我我在做什么错吗?
您的问题可能出在线路(或线路的一部分)上
Country = y.Country != null ? (Country)y.Country : (Country?)null
您在一种情况下将价值转换为国家/地区,还是在国家/地区? 在另一个。 也许您可以将值替换为-1,或者更可靠地将Customer.Country类型更改为Country? 而不是国家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.