2012-01-16 16 views
2

複合キーで多くの-多くのマッピング:
*ブランド
*商品コード
:(*キー表す) を流暢NHibernateは - 私は次のようにテーブル構造を持つアプリケーション開発してい

製品を 商品名

カテゴリー
*区分
CategoryNam製品のための私のマッピングで

public class Product 
{ 
    public int ProductID { get; set; } 
    public string Brand{ get; set; } 
    public string ProductName { get; set; } 

    public IEnumerable<Category> { get; set; } 
} 

public class Category 
{ 
    public int CategoryID { get; set; } 
    public string CategoryName { get; set; } 
} 

は、次のように
*区分
*商品コード

製品電子

製品のリスト

クラスでのProductID &ブランドの複合IDを持っています、私は

HasManyToMany(x => x.Categories).Table("ProductCategories") 
    .ParentKeyColumn(NameOf<Product>.Property(p => p.ProductID)) 
    .ChildKeyColumn(NameOf<Category>.Property(p => p.CategoryID)) 
    .Cascade.All(); 

したがって、基本的に、ProductCategoriesテーブルのProductIDに基づいてカテゴリを選択しようとしています... これは可能ですか?

However- 私のようなエラーを取得しています:あなたのコードは、ほぼ正しい

HasManyToMany<Category>(x => x.Categories) 
    .Table("ProductCategories") 
    .ParentKeyColumn("ProductID") 
    .ChildKeyColumn("CategoryID") 
+0

like-べきか? – Richard

+0

ありがとう – Alex

答えて

0

。問題は、.ParentKeyColumn(...)と.ChildKeyColumn(...)がDb列名を必要とすることです。文字列NameOf.Property(p => p.ProductID)が列名に対応するかどうかを確認します。また、明示的にカテゴリのタイプを指定してみてください。

HasManyToMany<Category>(x => x.Categories) 
    .Table("ProductCategories") // DB table name 
    .ParentKeyColumn("product_id") // column name 
    .ChildKeyColumn("category_id") // column name 

希望ですが、これが問題です。私は私のコードで全く同じマッピングを使用しています。

+0

{"外部キー(FKCDD3036FAD8EF816:ProductCategories [ProductID]))は、参照される主キー(Product [ProductID、Brand])と同じ列数でなければなりません"} – Alex

+0

コンポジットIDに対応するためにProductCategoriesテーブルに列Brandを追加しようとしましたか? – Richard

+0

はい、私はちょうどProductCategoriesにブランドの列を追加しました - まだ同じ問題 – Alex

0

:あなたはこれを使用する必要があり、その場合には[OK]をうまく

must have same number of columns as the referenced primary key (Product [ProductID, Brand])

1

商品は次に製品のリストで表は商品コード、ブランド及び区分の複合キーでなければならない商品コード&ブランドの複合IDを持っている場合。それ以外の場合はuがエラーになります:

とマッピングは、ちょうど私はあなたが上記のItemCategoriesを書いたときに、製品のリストを意味仮定チェックする

HasManyToMany<Category>(x => x.Categories) 
    .Table("ProductCategories") 
    .ParentKeyColumns.Add("product_id","Brand") 
    .ChildKeyColumn("category_id") 
関連する問題