Fluent nHibernate - 如何在连接表上映射非键列?
以 Fluent nHibernate 网站上提供的示例为例,我需要稍微扩展一下:
(来源: Fluentnhibernate.org)
我需要向 StoreProduct 表添加“数量”列。我如何使用 nHibernate 映射它?
为上面给定的场景提供了示例映射,但我不确定如何将 Quantity 列映射到 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");
}
}
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");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种建议是不使用 hasManyToMany 映射,并为 StoreProduct(Product 的子类)使用单独的映射类。
新的商店映射
NB 将 HasManyToMany 更改为 HasMany。
商店产品的新子类映射
新的 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
NB changed HasManyToMany to HasMany instead.
New sub class mapping for Store Product
New StoreProduct entity
Hope that helps.