2012-01-13 96 views
0

我有沿的线DB模式:功能NHibernate - 映射一个对多使用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以及...(反之亦然)

因此,查询产品,返回该品牌的ProductVariations列表... (注意,ProductVariation在类中没有属性,但它有colu mn用于映射)

ProductVariation.ID是非唯一的。 关键是ProductVariation.ID和ProductVariation.Brand(在数据库上)

+0

是product.id不唯一吗? – Firo 2012-01-14 23:33:20

+0

no-product.id和brand是复合的 – Alex 2012-01-16 09:59:05

回答

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