流利Nhibernate映射相关的项目 [英] Fluent Nhibernate mapping related items
问题描述
所以,这是我的项目:
pre code $ public $ Item
{
public virtual Guid ItemId {get; set;}
public virtual string Name {get; set;}
公共虚拟IList< Item> RelatedItems {get;设置;}
}
关联这些项目的表如下所示:
CREATE TABLE相关项目
(
相关项目唯一标识符DEFAULT(NEWID())不是NULL,
ItemId uniqueidentifier NOT NULL,
RelatedId uniqueidentifier NOT NULL,
CONSTRAINT PK_RelatedItems PRIMARY KEY CLUSTERED(RelatedItemId)
)
映射这个连接的最好方法是什么?
$ p $ HasManyToMany(x => x.RelatedTo)
.Table (RelatedItems)
.ParentKeyColumn(ItemId)
.ChildKeyColumn(RelatedItemId);
HasManyToMany(x => x.RelatedToMe)
。表(RelatedItems)
.ChildKeyColumn(ItemId)
.ParentKeyColumn(RelatedItemId );
$ b然后在我的类中,我有一个名为RelatedItems的只读集合,它连接两个列表并选择不同的项目返回。
I am trying to relate 2 items. I have a table that is simply an Id field, and then 2 columns for the Item Id's to relate. I want it to be a 2 way relationship - that is, if the items appear twice in the table, I only want one relationship connection back.
So, here's my item:
public class Item
{
public virtual Guid ItemId {get; set;}
public virtual string Name {get; set;}
public virtual IList<Item> RelatedItems {get; set;}
}
The table for relating the items looks like this:
CREATE TABLE RelatedItems
(
RelatedItemId uniqueidentifier DEFAULT(NEWID()) NOT NULL,
ItemId uniqueidentifier NOT NULL,
RelatedId uniqueidentifier NOT NULL,
CONSTRAINT PK_RelatedItems PRIMARY KEY CLUSTERED (RelatedItemId)
)
What is the best way to map this connection?
I had to do it the hard way (read HACK).
HasManyToMany(x => x.RelatedTo)
.Table("RelatedItems")
.ParentKeyColumn("ItemId")
.ChildKeyColumn("RelatedItemId");
HasManyToMany(x => x.RelatedToMe)
.Table("RelatedItems")
.ChildKeyColumn("ItemId")
.ParentKeyColumn("RelatedItemId");
Then in my class I have a read only collection called RelatedItems which joins the two lists and selects distinct items to return.
这篇关于流利Nhibernate映射相关的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!