gpt4 book ai didi

c# - 系统.InvalidOperationException : The specified cast from a materialized 'System.Int32' type to a nullable 'Country' type is not valid

转载 作者:行者123 更新时间:2023-12-02 05:12:18 25 4
gpt4 key购买 nike

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

System.InvalidOperationException: The specified cast from a materialized 'System.Int32' type to a nullable 'Country' type is not valid.

通过跟踪堆栈,下面的方法有问题——

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

在一种情况下,您将值转换到 Country 和 Country?在另一个。也许您可以将值替换为 -1,或者更可靠地,将 Customer.Country 类型更改为 Country?而不是国家/地区。

关于c# - 系统.InvalidOperationException : The specified cast from a materialized 'System.Int32' type to a nullable 'Country' type is not valid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15237170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com