2

個人のPCで正常に実行される特定の単体テストがありますが、TFSにテストを実行させると、次の例外が発生して失敗します-

System.InvalidOperationException:マテリアライズされた'System.Int32'型からnull許容の'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>

誰かが私が間違っていることを教えてもらえますか?

4

1 に答える 1

5

あなたの問題はおそらく線(または線の一部)にあります

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

あるケースではCountryに値をキャストし、Country?別の。おそらく、値を-1に置き換えるか、より確実に、Customer.CountryタイプをCountryに変更できますか?国の代わりに。

于 2013-03-06T01:19:57.097 に答える