Fluent NHibernate 仅为多个 HasMany 关系生成一列额外的列
我有一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我离基地太远了。我假设 FNH 正在为 HasMany 关系生成一个单独的表(事实并非如此)。此处找到正确的代码:Fluent NHibernate Self Referencing Many To Many
特定于我的问题,我创建了两个单独的表:
然后在我的映射类中:
这解决了所有问题。
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:
Then in my mapping class:
That fixes all the issues.