简体   繁体   English

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

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

Taking an example that is provided on the Fluent nHibernate website, I need to extend it slightly: 以Fluent nHibernate网站上提供的示例为例,我需要对其进行一些扩展:

替代文字
(source: fluentnhibernate.org ) (来源: fluentnhibernate.org

I need to add a 'Quantity' column to the StoreProduct table. 我需要在StoreProduct表中添加“数量”列。 How would I map this using nHibernate? 我将如何使用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: 针对上面的给定场景提供了一个示例映射,但是我不确定如何将“数量”列映射到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");
  }
}

One suggestion would be to not use the hasManyToMany mapping and have a separate mapping class for StoreProduct which is a subclass of Product. 一个建议是不要使用hasManyToMany映射,并为StoreProduct设置一个单独的映射类,该类是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. NB将HasManyToMany改为HasMany。

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. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 NHibernate 中的非键列配置 OneToOne 映射 - Configure OneToOne mapping with non-key column in NHibernate 如何将枚举映射为 (Fluent) NHibernate 中的外键? - How to map an enum as a foreign key in (Fluent) NHibernate? NHibernate Fluent多对多映射,在联结表中具有多列主键 - NHibernate Fluent many-to-many mapping with multicolumn primary key in junction table 将多个表映射到一个表会为流利的NHibernate中的每个表生成外键列 - Map Multiple Tables to One Table Produces Foreign Key Column for each in Fluent NHibernate 与EF6中非关键列的关系 - relationship to non-key column in EF6 流利的nHibernate将HasMany映射到没有主键的实体/表 - Fluent nHibernate map HasMany to Entity/Table with no Primary Key 使用NHibernate在联结表中创建一个复合主键 - Create a Composite Primary Key in the junction table with NHibernate 在表中使用不带主键/唯一键的Fluent nHibernate - Using Fluent nHibernate without primary/unique key column in the table 流利的NHibernate-如何映射到引用了查询表的表 - Fluent NHibernate - How to map to table which has reference to a lookup table 如何在NHibernate hbm xml(或在fluent-nhibernate类映射中)映射也是主键的组件? - How do you map a component that is also a primary key in NHibernate hbm xml (or in a fluent-nhibernate class map)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM