流利Nhibernate映射相关的项目 [英] Fluent Nhibernate mapping related items

查看:127
本文介绍了流利Nhibernate映射相关的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图把2个项目联系起来。我有一个表,只是一个Id字段,然后2个项目Id的列相关。我希望它是一种双向关系 - 也就是说,如果项目在表格中出现两次,我只想要一个关系连接回来。



所以,这是我的项目:

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屋!

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