Fluent NHibernate 仅为多个 HasMany 关系生成一列额外的列

发布于 2024-10-18 15:17:42 字数 956 浏览 1 评论 0原文

我有一个基于 NHibernate 构建的同义词库应用程序。数据模型非常简单:

public class Word
{
    public virtual int Id { get; set; }
    public virtual string Text { get; set; }
    public virtual IList<Word> Synonyms { get; set; }
    public virtual IList<Word> Antonyms { get; set; }
}

因此每个单词都有一个同义词列表和一个反义词列表。我认为映射也很简单:

public class WordMapping : ClassMap<Word>
{
    public WordMapping()
    {
        Id(x => x.Id);
        Map(x => x.Text);
        HasMany(x => x.Synonyms).Cascade.AllDeleteOrphan();
        HasMany(x => x.Antonyms).Cascade.AllDeleteOrphan();
    }
}

当我查看 MS SQL Server 2008 中生成的表时,我只看到两个 HasMany 关系的一个关键列:

Id        int
Text      nvarchar(255)
Word_id   int

我认为这会导致在插入时发生一些奇怪的事情数据。当我在进行一些插入后获得列表时,同义词反义词都包含相同的单词,但是单词只是这两个列表中应该包含的内容看似任意的子集。

我的问题定位正确吗?如果是这样,我该如何修复它?

I have a thesaurus application built on NHibernate. The data model is very straight-forward:

public class Word
{
    public virtual int Id { get; set; }
    public virtual string Text { get; set; }
    public virtual IList<Word> Synonyms { get; set; }
    public virtual IList<Word> Antonyms { get; set; }
}

So each word has a list of synonyms and a list of antonyms. I thought the mapping would be straight-forward as well:

public class WordMapping : ClassMap<Word>
{
    public WordMapping()
    {
        Id(x => x.Id);
        Map(x => x.Text);
        HasMany(x => x.Synonyms).Cascade.AllDeleteOrphan();
        HasMany(x => x.Antonyms).Cascade.AllDeleteOrphan();
    }
}

When I look at the table generated in MS SQL Server 2008, I only see one key column for the two HasMany relations:

Id        int
Text      nvarchar(255)
Word_id   int

I think this is causing some weird things to happen when I insert data. When I get the lists after doing some insertions, both Synonyms and Antonyms contain the same Words, but the Words are just seemingly-arbitrary subsets of what should be in the two lists.

Do I have the problem pegged right? If so, how can I fix it?

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

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

发布评论

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

评论(1

失而复得 2024-10-25 15:17:42

我离基地太远了。我假设 FNH 正在为 HasMany 关系生成一个单独的表(事实并非如此)。此处找到正确的代码:Fluent NHibernate Self Referencing Many To Many

特定于我的问题,我创建了两个单独的表:

-- Synonyms table --
WordId        int
SynonymId     int

-- Antonyms table --
WordId        int
AntonymId     int

然后在我的映射类中:

public class WordMapping : ClassMap<Word>
{
    public WordMapping()
    {
        Id(x => x.Id);
        Map(x => x.Text);
        HasManyToMany(x => x.Synonyms).ParentKeyColumn("WordId")
                                              .ChildKeyColumn("SynonymId")
                                              .Table("Synonyms");
        HasManyToMany(x => x.Antonyms).ParentKeyColumn("WordId")
                                              .ChildKeyColumn("AntonymId")
                                              .Table("Antonyms");
    }
}

这解决了所有问题。

I was way off base. I assumed FNH was generating a separate table for the HasMany relationships (it wasn't). Proper code found here: Fluent NHibernate Self Referencing Many To Many

Specific to my problem, I created two separate tables:

-- Synonyms table --
WordId        int
SynonymId     int

-- Antonyms table --
WordId        int
AntonymId     int

Then in my mapping class:

public class WordMapping : ClassMap<Word>
{
    public WordMapping()
    {
        Id(x => x.Id);
        Map(x => x.Text);
        HasManyToMany(x => x.Synonyms).ParentKeyColumn("WordId")
                                              .ChildKeyColumn("SynonymId")
                                              .Table("Synonyms");
        HasManyToMany(x => x.Antonyms).ParentKeyColumn("WordId")
                                              .ChildKeyColumn("AntonymId")
                                              .Table("Antonyms");
    }
}

That fixes all the issues.

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