C# Fluent NHibernate-使用2列将一个映射到多个

C# Fluent NHibernate-使用2列将一个映射到多个,c#,fluent-nhibernate,fluent-nhibernate-mapping,C#,Fluent Nhibernate,Fluent Nhibernate Mapping,我有一个db模式,大致如下: 产品 身份证 产品名称 描述 商店品牌 产品变化 变量 产品ID 大小 商店品牌 价格 可以预见,类看起来有点像这样: public class Product { public virtual int ID { get; set; } public virtual string ProductName { get; set; } public virtual string Description { get; set; } public v

我有一个db模式,大致如下:

产品

  • 身份证
  • 产品名称
  • 描述
  • 商店品牌
产品变化

  • 变量
  • 产品ID
  • 大小
  • 商店品牌
  • 价格
可以预见,类看起来有点像这样:

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");
  }
}
公共类产品
{
公共虚拟整数ID{get;set;}
公共虚拟字符串ProductName{get;set;}
公共虚拟字符串描述{get;set;}
公共虚拟字符串StoreBrand{get;set;}
公共虚拟IEnumerable变体{get;set;}
}
公共类产品变化
{
公共虚拟整数变量ID{get;set;}
公共虚拟int ProductID{get;set;}
公共虚拟产品产品{get;set;}
公共虚拟字符串大小{get;set;}
公共虚拟双价格{get;set;}
}
我有这样的映射类:

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");
  }
}
公共类ProductMapper:ClassMap
{
公共产品映射器()
{
Id(x=>x.Id);
地图(x=>x.ProductName);
映射(x=>x.Description);
地图(x=>x.StoreBrand);
有许多(x=>x.Variations)
.KeyColumn(“ProductID”);
}
}
公共类ProductVariationMapper:ClassMap
{
公共产品变化()
{
Id(x=>x.Id);
Map(x=>x.ProductID);
地图(x=>x.Size);
地图(x=>x.Price);
参考(x=>x.Product)
.列(“产品ID”);
}
}
这是一种工作

然而,我需要做的是将产品。品牌与产品变化。品牌以及。。。(反之亦然)

所以查询产品,返回该品牌的产品变体列表。。。 (注意,ProductVariation在类中没有属性,但它有用于映射的列)

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

公共类产品
{
公共虚拟整数ID{get;set;}
公共虚拟字符串StoreBrand{get;set;}
公共虚拟字符串ProductName{get;set;}
公共虚拟字符串描述{get;set;}
公共虚拟IEnumerable变体{get;set;}
公共覆盖等于(对象obj)
{
收益等于(obj作为产品)
}
公共覆盖等于(产品其他)
{
return(other!=null)&&(Id==other.Id)&&(StoreBrand==other.StoreBrand);
}
公共覆盖GetHashCode()
{
未经检查
{
返回Id.GetHashCode()*397+StoreBrand.GetHashCode();
}
}
}
公共类产品变化
{
公共虚拟整数ID{get;set;}
公共虚拟产品产品{get;set;}
公共虚拟字符串大小{get;set;}
公共虚拟双价格{get;set;}
}
公共类ProductMapper:ClassMap
{
公共产品映射器()
{
//Id本身不是唯一的,因此compositeId
复合ID()
.KeyProperty(x=>x.ID)
.KeyProperty(x=>x.StoreBrand);
地图(x=>x.ProductName);
映射(x=>x.Description);
有许多(x=>x.Variations)
.KeyColumn(“ProductID”、“StoreBrand”);
}
}
公共类ProductVariationMapper:ClassMap
{
公共产品变化()
{
Id(x=>x.Id);
地图(x=>x.Size);
地图(x=>x.Price);
参考(x=>x.Product)
.栏(“产品ID”、“商店品牌”);
}
}

product.id是否也是非唯一的?否-product.id和brand是复合的