问题描述
以Fluent nHibernate网站上提供的示例为例,我需要稍微扩展一下:
Taking an example that is provided on the Fluent nHibernate website, I need to extend it slightly:
我需要在StoreProduct表中添加一个Quantity列。我将如何使用nHibernate映射这个?
I need to add a 'Quantity' column to the StoreProduct table. How would I map this using nHibernate?
上面给出的场景提供了一个示例映射,但是我不知道如何获取Quantity列映射Product类的属性:
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");
}
}
推荐答案
建议不要使用hasManyToMany映射,并为StoreProduct(它是Product的子类)提供单独的映射类。
One suggestion would be to not use the hasManyToMany mapping and have a separate mapping class for StoreProduct which is a subclass of 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。 / strong>
NB changed HasManyToMany to HasMany instead.
商店产品的新子类映射
public class StoreProductMap : SubclassMap<StoreProduct>
{
References(x=>x.Store);
Map(x=>x.Quantity);
}
新StoreProduct实体
New StoreProduct entity
public class StoreProduct : Product
{
public virtual Store Store {get;set;}
public virtual int Quantity {get;set;}
}
希望有帮助。
这篇关于Fluent nHibernate - 如何在联结表上映射非键列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!