System.InvalidOperationException:从具体化的"System.Int3

System.InvalidOperationException: The specified cast from a materialized 'System.Int32' type to a nullable 'Country' type is not valid
2021-11-25
  •  译文(汉语)
  •  原文(英语)

我有一个特定的单元测试在我的个人 PC 上运行良好,但是每当我让 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();
    }
}

如果它是相关的(怀疑是),我的 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>

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

解决过程1

您的问题可能与线路(或线路的一部分)有关

Country = y.Country != null ? (Country)y.Country : (Country?)null

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

I have a particular unit test that runs fine on my personal PC, but whenever I get TFS to run the test, it fails with the following exception -

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

By following the stack trace, it has a problem with the following method -

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();
    }
}

In case it's relevant (doubt it is), my EFFunctions class is defined as -

public static class EFFunctions
{
    [EdmFunction("Model", "ConvertToInt32")]
    public static int ConvertToInt32(string text)
    {
        var result = string.IsNullOrEmpty(text) ? 0 : Convert.ToInt32(text);

        return result;
    }
}

And my .edmx has the following in it -

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

Is anyone able to tell me what I'm doing wrong?

Solutions1

Your problem is probably with the line (or part of line)

Country = y.Country != null ? (Country)y.Country : (Country?)null

You cast the value to Country in one case and Country? in another. Perhaps you could replace the value with -1, or, probably more reliably, change the Customer.Country type to Country? instead of Country.

转载于:https://stackoverflow.com/questions/15237170/system-invalidoperationexception-the-specified-cast-from-a-materialized-system

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入