Fluent Nhibernate 映射相关项目
我正在尝试将两个项目联系起来。我有一个表,它只是一个 Id 字段,然后有 2 列用于关联项目 Id。我希望它是一种双向关系 - 也就是说,如果项目在表中出现两次,我只想要一个关系连接。
所以,这是我的项目:
public class Item
{
public virtual Guid ItemId {get; set;}
public virtual string Name {get; set;}
public virtual IList<Item> RelatedItems {get; set;}
}
用于关联项目的表格如下所示:
CREATE TABLE RelatedItems
(
RelatedItemId uniqueidentifier DEFAULT(NEWID()) NOT NULL,
ItemId uniqueidentifier NOT NULL,
RelatedId uniqueidentifier NOT NULL,
CONSTRAINT PK_RelatedItems PRIMARY KEY CLUSTERED (RelatedItemId)
)
映射此连接的最佳方法是什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用“HasMany”映射。我认为以下方法可能有效:
您始终可以对 Id 字段使用约定。并考虑 HasMany 集合上的级联删除选项。
You need to use a "HasMany" mapping. I think the following may work:
You can always use a convention for the Id field. And consider cascade delete options on the HasMany collection.
我必须以艰难的方式做到这一点(阅读HACK)。
然后在我的班级中,我有一个名为“RelatedItems”的只读集合,它连接两个列表并选择要返回的不同项目。
I had to do it the hard way (read HACK).
Then in my class I have a read only collection called RelatedItems which joins the two lists and selects distinct items to return.