功能NHibernate - 如何映射在一个路口表中的非键列? [英] Fluent nHibernate - How to map a non-key column on a junction table?
问题描述
拍摄所提供的功能NHibernate网站上的一个例子,我需要稍微扩展它:
我需要一个量列添加到StoreProduct表。我将如何使用NHibernate映射呢?
这是映射示例提供上述给定的情景,但我不知道我怎么会得到数量列映射到在产品类中的属性:
公共类StoreMap:ClassMap<商店和GT;
{
公共StoreMap()
{
ID(X => x.Id);
地图(X => x.Name);
的hasMany(X => x.Employee)
.Inverse()
.Cascade.All();
HasManyToMany(X => x.Products)
.Cascade.All()
。表(StoreProduct);
}
}
一建议是不要使用hasManyToMany映射并有StoreProduct一个单独的映射类,它是产品的一个子类。
新店映射
公共类StoreMap:ClassMap<商店和GT;
{
公共StoreMap()
{
ID(X => x.Id);
地图(X => x.Name);
的hasMany(X => x.Employee)
.Inverse()
.Cascade.All();
的hasMany(X => x.Products)
.Cascade.All();
}
}
NB改变HasManyToMany而不是的hasMany。
新的子类映射存储产品
公开类StoreProductMap:SubclassMap< StoreProduct>
{
引用(X => x.Store);
地图(X => x.Quantity);
}
新StoreProduct实体
公共类StoreProduct:产品
{
公共虚拟店中店{获取;集;}
公共虚拟INT数量{获取;集;}
}
希望有所帮助。
Taking an example that is provided on the Fluent nHibernate website, I need to extend it slightly:
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");
}
}
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.
这篇关于功能NHibernate - 如何映射在一个路口表中的非键列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!