开发者

Fluent NHibernate grouping multiple rows to collection property

开发者 https://www.devze.com 2023-01-11 02:04 出处:网络
I have classes like public class Content { // ... public virtual IList<Detail> { get; set; } } public class Detail

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. 开发者_如何学Python

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消