2012-01-13 19 views
0

を使用して1対多マッピング:流暢NHibernateは - 私はのラインに沿ってDBスキーマを有する2列

商品

  • ID
  • 商品名
  • 説明
  • StoreBrand

ProductVariation

  • VariationID
  • のProductID
  • サイズ
  • StoreBrand
  • 価格

クラスは、予想通り、見て、このようなビット:

public class Product 
{ 
    public virtual int ID { get; set; } 
    public virtual string ProductName { get; set; } 
    public virtual string Description { get; set; } 
    public virtual string StoreBrand { get; set; } 

    public virtual IEnumerable<ProductVariation> Variations { get; set; } 
} 

public class ProductVariation 
{ 
    public virtual int VariationID { get; set; } 
    public virtual int ProductID { get; set; } 

    public virtual Product Product {get; set;}  

    public virtual string Size { get; set; } 
    public virtual double Price { get; set; } 
} 

私はこのようなマッピングクラス持っている:私は何をする必要があるか、しかし

public class ProductMapper : ClassMap<Product> 
{ 
    public ProductMapper() 
    { 
    Id(x => x.ID); 
    Map(x => x.ProductName); 
    Map(x => x.Description); 
    Map(x => x.StoreBrand); 

    HasMany(x => x.Variations) 
     .KeyColumn("ProductID"); 
    } 
} 

public class ProductVariationMapper : ClassMap<ProductVariation> 
{ 
    public ProductVariation() 
    { 
    Id(x => x.ID); 
    Map(x => x.ProductID); 
    Map(x => x.Size); 
    Map(x => x.Price); 

    References(x => x.Product) 
     .Column("ProductID"); 
    } 
} 

これは作業の一種である...

は一緒Product.BrandsをネクタイでProductVariation.Brands ...

Productを照会すると、そのブランドのProductVariationsのリストが返されます。 (ProductVariationにはクラス内のプロパティはありませんが、それはコルを持っていますマッピングの場合はmn)

ProductVariation.IDは一意ではありません。 キーはProductVariation.IDおよびProductVariation.Brand(データベース上)です

+0

はproduct.idも固有ではありませんか? – Firo

+0

no-product.idとブランドはコンポジットです – Alex

答えて

0
public class Product 
{ 
    public virtual int ID { get; set; } 
    public virtual string StoreBrand { get; set; } 

    public virtual string ProductName { get; set; } 
    public virtual string Description { get; set; } 

    public virtual IEnumerable<ProductVariation> Variations { get; set; } 

    public override Equals(object obj) 
    { 
     return Equals(obj as Product) 
    } 

    public override Equals(Product other) 
    { 
     return (other != null) && (Id == other.Id) && (StoreBrand == other.StoreBrand); 
    } 

    public override GetHashCode() 
    { 
     unchecked 
     { 
      return Id.GetHashCode() * 397 + StoreBrand.GetHashCode(); 
     } 
    } 
} 

public class ProductVariation 
{ 
    public virtual int ID { get; set; } 

    public virtual Product Product {get; set;}  

    public virtual string Size { get; set; } 
    public virtual double Price { get; set; } 
} 

public class ProductMapper : ClassMap<Product> 
{ 
    public ProductMapper() 
    { 
     // Id alone is not unique, hence compositeId 
     CompositeId() 
      .KeyProperty(x => x.ID) 
      .KeyProperty(x => x.StoreBrand); 

     Map(x => x.ProductName); 
     Map(x => x.Description); 

     HasMany(x => x.Variations) 
      .KeyColumn("ProductID", "StoreBrand"); 
    } 
} 

public class ProductVariationMapper : ClassMap<ProductVariation> 
{ 
    public ProductVariation() 
    { 
     Id(x => x.ID); 

     Map(x => x.Size); 
     Map(x => x.Price); 

     References(x => x.Product) 
      .Column("ProductID", "StoreBrand"); 
    } 
} 
関連する問題