0

このスレッドの問題をデバッグする際: LINQ でネストされたコレクションをクエリするときの InvalidCastExceptionカテゴリ EntitySet の設定方法に問題があることがわかりました。カテゴリを選択し、この例外をスローして何が起こっているかを確認した後、次のようになります。

throw new Exception("CID: " + cat.CategoryID +
  " LCID: "        + cat.LocalizedCategories.First().LocalizedCategoryID +
  " CID from LC: " + cat.LocalizedCategories.First().Category.CategoryID);

CID: 352 LCID: 352 LC からの CID: 191

LINQ の方法に応じて CategoryID の値が異なる原因となる間違っていることは何ですか? LocalizedCategoryID と同じ値ではなく、191 にする必要があります。

これは、カテゴリを取得するために使用するコードです。

int categoryId = 352; // In reality this comes from a parameter and is supposed
                      // to be 191 to get the Category.
var cat = categoriesRepository.Categories.First(c => c.CategoryID == categoryId);

これは、いくつかの無関係なものを削除した私のドメイン オブジェクトです。

[Table(Name = "products")]
public class Product
{
    [HiddenInput(DisplayValue = false)]
    [Column(Name = "id", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int ProductID { get; set; }

    [Required(ErrorMessage = "Please enter a product name")]
    [Column]
    public string Name { get; set; }

    [Required(ErrorMessage = "Please enter a description")]
    [DataType(DataType.MultilineText)]
    [Column(Name = "info")]
    public string Description { get; set; }

    private EntitySet<Category> _Categories = new EntitySet<Category>();
    [System.Data.Linq.Mapping.Association(Storage = "_Categories", OtherKey = "CategoryID")]
    public ICollection<Category> Categories
    {
        get { return _Categories; }
        set { _Categories.Assign(value); }
    }
}

[Table(Name = "products_types")]
public class Category
{
    [HiddenInput(DisplayValue = false)]
    [Column(Name = "id", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int CategoryID { get; set; }

    public string NameByCountryId(int countryId)
    {
        return _LocalizedCategories.Single(lc => lc.CountryID == countryId).Name;
    }

    private EntitySet<LocalizedCategory> _LocalizedCategories = new EntitySet<LocalizedCategory>();
    [System.Data.Linq.Mapping.Association(Storage = "_LocalizedCategories", OtherKey = "LocalizedCategoryID")]
    public ICollection<LocalizedCategory> LocalizedCategories
    {
        get { return _LocalizedCategories; }
        set { _LocalizedCategories.Assign(value); }
    }

    private EntitySet<Product> _Products = new EntitySet<Product>();
    [System.Data.Linq.Mapping.Association(Storage = "_Products", OtherKey = "ProductID")]
    public ICollection<Product> Products
    {
        get { return _Products; }
        set { _Products.Assign(value); }
    }
}

[Table(Name = "products_types_localized")]
public class LocalizedCategory
{
    [HiddenInput(DisplayValue = false)]
    [Column(Name = "id", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
    public int LocalizedCategoryID { get; set; }

    [Column(Name = "products_types_id")]
    private int CategoryID;
    private EntityRef<Category> _Category = new EntityRef<Category>();
    [System.Data.Linq.Mapping.Association(Storage = "_Category", ThisKey = "CategoryID")]
    public Category Category
    {
        get { return _Category.Entity; }
        set { _Category.Entity = value; }
    }

    [Column(Name = "country_id")]
    public int CountryID { get; set; }

    [Column]
    public string Name { get; set; }
}
4

1 に答える 1

1

これは(クラスでCategory)奇妙に見えます:

[System.Data.Linq.Mapping.Association(Storage = "_LocalizedCategories",
    OtherKey = "LocalizedCategoryID" )] // ????
public ICollection<LocalizedCategory> LocalizedCategories

Categoryのコレクションがあります。LocalizedCategoryこれは、データベース内でテーブルproducts_types_localizedに外部キーがあることを意味しますCategoryID。そのフィールドは「OtherKey」である必要があります。このマッピングはどのように生成されましたか?

于 2012-09-24T19:54:22.883 に答える