Fluent nHibernate - How to map a non-key column on a junction table?

柔情痞子 提交于 2019-12-08 04:37:46

问题


Taking an example that is provided on the Fluent nHibernate website, I need to extend it slightly:


(source: fluentnhibernate.org)

I need to add a 'Quantity' column to the StoreProduct table. How would I map this using nHibernate?

An example mapping is provided for the given scenario above, but I'm not sure how I would get the Quantity column to map to a property on the Product class:

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:


One suggestion would be to not use the hasManyToMany mapping and have a separate mapping class for StoreProduct which is a subclass of Product.

New Store Mapping

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 changed HasManyToMany to HasMany instead.

New sub class mapping for Store Product

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

   Map(x=>x.Quantity);
}

New StoreProduct entity

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

Hope that helps.



来源:https://stackoverflow.com/questions/3003025/fluent-nhibernate-how-to-map-a-non-key-column-on-a-junction-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!