FluentNHibernate Lookup Table

喜欢而已 提交于 2019-12-13 00:25:25

问题


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.


回答1:


You need a Many-to-Many relationship.

Try:

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


来源:https://stackoverflow.com/questions/1494055/fluentnhibernate-lookup-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!