如何使用多对多关系在NHibernate中映射字符串集合?

时间:2009-07-10 18:53:48

标签: nhibernate nhibernate-mapping

鉴于以下固定域模型:

public class Sentence {
    public int Id { get; private set; }
    public string Author { get; set; }
    public string[] Words { get; set; }
}

以下提出的规范化数据库模式:

create table Sentence (
    Id int identity(1,1) not null primary key,
    Author varchar(450)
)
create table Word (
    Id int identity(1,1) not null primary key,
    Value varchar(450) not null unique
)
create table Sentence_Word (
    SentenceId int foreign key references Sentence (Id),
    WordId int foreign key references Word (Id),
    SortIndex int not null,
    primary key (SentenceId, WordId),
    unique (SentenceId, SortIndex)
)

您将如何创建NHibernate映射文件?

如果我们可以添加其他域实体Word,然后将Words上的Sentence属性更改为Word[]而不是string[](它'是标准的多对多实体关系),但属性的string[]类型是必需的,不能更改。

这个问题类似于another StackOverflow question,除了需要使用多对多关系进行正确的数据规范化(单词集相对较小而将它们组合成句子很大),所以目前还不清楚如何应用基于标准价值的馆藏。

1 个答案:

答案 0 :(得分:0)

您可以按照建议添加域实体Word,实现Word on Sentence的集合,并继续将Word []数组公开为string []。每次访问属性时,只需从Word集合动态构建Word []数组。如果性能是一个问题,只有在Word集合发生更改时才能缓存并重建Word []数组。

  

如果我们可以添加另一个域实体Word,然后将Sentence上的Words属性更改为Word []而不是string [](这将是标准的多对多实体关系),这将很容易,但是属性的string []类型是必需的,不能更改。

相关问题