[英]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.