Fluent nHibernate - 如何在联结表上映射非键列?

时间:2010-06-09 04:05:42

标签: c# database nhibernate orm fluent-nhibernate

以Fluent nHibernate网站上提供的示例为例,我需要稍微扩展一下:

alt text http://wiki.fluentnhibernate.org/images/2/24/FirstProjectSchema.png

我需要在StoreProduct表中添加一个'Quantity'列。我如何使用nHibernate映射它?

为上面给出的场景提供了一个示例映射,但我不确定如何将Quantity列映射到Product类上的属性:

public class StoreMap : ClassMap<Store>
{
  public StoreMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    HasMany(x => x.Employee)
      .Inverse()
      .Cascade.All();
    HasManyToMany(x => x.Products)
     .Cascade.All()
     .Table("StoreProduct");
  }
}

1 个答案:

答案 0 :(得分:1)

一个建议是不使用hasManyToMany映射,并为StoreProduct提供单独的映射类,StoreProduct是Product的子类。

新商店映射

public class StoreMap : ClassMap<Store>
{
  public StoreMap()
  {
    Id(x => x.Id);
    Map(x => x.Name);
    HasMany(x => x.Employee)
      .Inverse()
      .Cascade.All();
    HasMany(x => x.Products)
     .Cascade.All();
  }
}

NB将HasManyToMany更改为HasMany。

商店产品的新子类映射

public class StoreProductMap : SubclassMap<StoreProduct>
{
   References(x=>x.Store);

   Map(x=>x.Quantity);
}

新的StoreProduct实体

public class StoreProduct : Product
{
    public virtual Store Store {get;set;}
    public virtual int Quantity {get;set;}
}

希望有所帮助。