Fluent NHibernate-使用2列映射一对多

本文关键字:映射 一对多 2列 使用 NHibernate- Fluent | 更新日期: 2023-09-27 18:20:18

我有一个数据库模式,其行数如下:

产品

  • ID
  • ProductName
  • 说明
  • StoreBrand

产品变化

  • 变量ID
  • 产品ID
  • 尺寸
  • 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,返回该品牌的ProductVariations列表。。。(注意,ProductVariation在类中没有属性,但它有用于映射的列)

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

Fluent NHibernate-使用2列映射一对多

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