FluentNHibernate查找表 [英] FluentNHibernate Lookup Table

查看:137
本文介绍了FluentNHibernate查找表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我创建了一个Product类,其中包含一个附件。

表格设置:

每个附件只是查找表中引用的另一个产品。  Product 
-------
ProductID int
名称varchar(200)

AccessoryProduct
---- ------------
ID int
ParentProductID int
ChildProductID int

我希望能够以如下方式访问此列表的配件:

  foreach(product.Accessories中的Product p)
string s = p.Name;

我难以忍受的部分是这个查找的FluentNHibernate映射。在我的ProductMap类中,我有以下映射:

  Id(x => x.ProductID); 
Map(x => x.Name);
HasMany(x => x.Accessories)
.Table(AccessoryProduct)
.KeyColumn(ParentProductID)
.Cascade.None()
.Inverse()
.LazyLoad();

当前正在创建一个查询,查找产品表中的ParentProductID而不是查找表(AccessoryProduct)。

是否有一个简单的方法,我错过了一个查询表的流畅映射?



任何帮助,即使它涉及xml映射,赞赏。我应该能够弄清楚流利的一面。

解决方案

您需要一个多对多的关系。


$ b

尝试:

  HasManyToMany(x => x.Accessories)
。表(AccessoryProduct)
.ParentKeyColumn(ParentProductID)
.ChildKeyColumn(ChildProductID)
.Cascade.None()
.Inverse()
.LazyLoad();


This is possibly an easy one that I can't seem to get past.

I've created a "Product" class which has a list of "Accessories". Each "Accessory" is just another product referenced by a lookup table.

Table setup:

Product
-------
ProductID int
Name varchar(200)

AccessoryProduct
----------------
ID int
ParentProductID int
ChildProductID int

I'd like to be able to access this list of accessories in a manner such as:

foreach(Product p in product.Accessories)
 string s = p.Name;

The part I'm stumped on is the FluentNHibernate mapping for this lookup. Within my ProductMap class, I have the following mapping:

Id(x => x.ProductID);
Map(x => x.Name);
HasMany(x => x.Accessories)
 .Table("AccessoryProduct")
 .KeyColumn("ParentProductID")
 .Cascade.None()
 .Inverse()
 .LazyLoad();

This is currently creating a query that looks for "ParentProductID" within the Product table instead of the lookup table(AccessoryProduct).

Is there a simple method I'm missing that allows for a fluent mapping of a lookup table?

Any assistance is appreciated, even if it involves the xml mapping. I should be able to figure out the fluent side.

解决方案

You need a Many-to-Many relationship.

Try:

 HasManyToMany(x => x.Accessories)
 .Table("AccessoryProduct")
 .ParentKeyColumn("ParentProductID")
 .ChildKeyColumn("ChildProductID")
 .Cascade.None()
 .Inverse()
 .LazyLoad();

这篇关于FluentNHibernate查找表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆