Fluent NHibernate 将多行分组到集合属性

发布于 2024-09-14 00:52:51 字数 1070 浏览 2 评论 0原文

我有类似的类

public class Content
{
    // ...
    public virtual IList<Detail> { get; set; }
}
public class Detail
{
    // ...
    public virtual Content Content { get; set; }
    public virtual string Name { get; set; }
}
public class TagDetail : Detail
{
    public virtual Tag Value { get; set; }
}
public class TagCollectionDetail : Detail
{
    public virtual IList<Tag> Value{ get; set; }
}

,我想将这些详细信息映射到表

Details -table
contentId    name    type            tagId
1            Tag     Tag             2
2            Tags    TagCollection   1
2            Tags    TagCollection   3
2            Tags    TagCollection   6

是否可以使用 Fluent NHibernate 将多行分组到一个对象(以及如何)? 我知道像这样重复信息(Detail.Name、Detail.Type)是一件坏事,但搜索会容易得多。

或者我必须将其映射到两个表中?

Details -table
contentId    name    type            tagId
1            Tag     Tag             2
2            Tags    TagCollection

DetailsCollection -table
detailId    tagId
2           1
2           3
2           6

I have classes like

public class Content
{
    // ...
    public virtual IList<Detail> { get; set; }
}
public class Detail
{
    // ...
    public virtual Content Content { get; set; }
    public virtual string Name { get; set; }
}
public class TagDetail : Detail
{
    public virtual Tag Value { get; set; }
}
public class TagCollectionDetail : Detail
{
    public virtual IList<Tag> Value{ get; set; }
}

and I would like to map those details to table

Details -table
contentId    name    type            tagId
1            Tag     Tag             2
2            Tags    TagCollection   1
2            Tags    TagCollection   3
2            Tags    TagCollection   6

Is it possible to group multiple rows to one object with Fluent NHibernate (and how)?
I know it's a bad thing to repeat information (Detail.Name, Detail.Type) like that, but searching would be much easier.

Or do I have to map it into two tables?

Details -table
contentId    name    type            tagId
1            Tag     Tag             2
2            Tags    TagCollection

DetailsCollection -table
detailId    tagId
2           1
2           3
2           6

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不一样的天空 2024-09-21 00:52:51

我将其建模如下:

Detail (table)
    - DetailId (col), PK
    - ContentId (col), FK to Content table
    - Name (col)

Tag (table)
    - TagId, PK
    - Other tag columns

TagDetail (table), only 1 row per Detail
    - TagDetailId (col), PK
    - DetailId (col), FK to Detail table
    - TagId, FK to Tag table

TagCollectionDetail (table), there would be many rows of this per Detail
    - TagCollectionDetailId, PK
    - DetailId (col), FK to Detail table
    - TagId, FK to Tag table

PK = 主键,FK = 外键。我将这些列设置为数字/整数类型,它会自动递增。对于没有主键的表,NHibernate 不能很好地工作(或者根本不可能)。

再想一想,我不明白为什么你有 TagDetail 和 TagCollectionDetail。 TagDetail 是 TagCollectionDetail 的特例,其中只有 1 个标签。我认为你可以摆脱 TagDetail。

I would model this as follows:

Detail (table)
    - DetailId (col), PK
    - ContentId (col), FK to Content table
    - Name (col)

Tag (table)
    - TagId, PK
    - Other tag columns

TagDetail (table), only 1 row per Detail
    - TagDetailId (col), PK
    - DetailId (col), FK to Detail table
    - TagId, FK to Tag table

TagCollectionDetail (table), there would be many rows of this per Detail
    - TagCollectionDetailId, PK
    - DetailId (col), FK to Detail table
    - TagId, FK to Tag table

PK = Primary Key, FK = Foreign Key. I would set these columns to a number/integer type, which gets auto-incremented. NHibernate doesn't work very well (or at all maybe) with tables without primary keys.

Thinking about this even more, I don't understand why you have TagDetail and TagCollectionDetail. TagDetail is a special case of TagCollectionDetail, where there is only 1 tag. I think you can get rid of TagDetail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文