NHibernate 在遗留数据库上如何告诉 NHibernate 哪个是父表
我在旧数据库中有两个表...
tblParentTable (int id, string specialIdentifier, ...)
tblChildTable (int id, string specialIdentifier, ...)
如您所见,添加了一个自动递增的int id,但是子表使用旧的字符串主键进行连接(实际上,specialIdentifier是tblParentTable的主键,以及 tblChildTable 上的主键和外键)。
因此,我创建了域对象和 Fluent NHibernate 映射,但由于这种奇怪的模式,NHibernate 认为它需要先保存 tblChildTable 记录,然后再保存 tblParentTable - 这会导致外键约束错误。
我如何向 NHibernate 提示 tblParentTable 是父表并且需要首先保存?
这是映射...
public ParentMap()
{
Table("tblParentTable");
Id(x => x.Id).Column("id");
Map(x => x.SpecialIdentifier);
References(x => x.Child).Column("specialIdentifier");
}
public ChildMap()
{
Table("tblChildTable");
Id(x => x.Id).Column("id");
Map(x => x.SpecialIdentifier);
References(x => x.Child).Column("specialIdentifier");
}
如果您认为我遗漏了一些重要的内容,请随时询问更多信息。
谢谢
I have two tables in a legacy database...
tblParentTable (int id, string specialIdentifier, ...)
tblChildTable (int id, string specialIdentifier, ...)
As you can see, an auto-incrementing int id has been added, but the child table is joined using the old string primary key (in fact, specialIdentifier is the primary key of tblParentTable, and the primary and foreign key on tblChildTable).
So I have created domain objects and Fluent NHibernate maps, but because of this odd schema, NHibernate thinks that it needs to save the tblChildTable record first, before it saves tblParentTable - this results in a foreign key constraint error.
How can I hint to NHibernate that tblParentTable is the parent and needs to be saved first?
Here is are the mappings...
public ParentMap()
{
Table("tblParentTable");
Id(x => x.Id).Column("id");
Map(x => x.SpecialIdentifier);
References(x => x.Child).Column("specialIdentifier");
}
public ChildMap()
{
Table("tblChildTable");
Id(x => x.Id).Column("id");
Map(x => x.SpecialIdentifier);
References(x => x.Child).Column("specialIdentifier");
}
Please feel free to ask for more information if you think I am missing something important.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不使用流利;相反,我通过手动创建 xml 映射文件来创建映射。
我相信关联的“反向”属性允许您指定关联的“所有者”(父级)。由于我不使用 flutter,我不知道如何在映射中指定它,所以我无法给你一个例子。
另外,在 Xml 映射中,我将使用“property-ref”属性来指示“specialIdentifier”是关系使用的列。
I don't use fluent; instead I create my mappings by creating the xml mapping files manually.
I believe that the 'inverse' property on the association allows you to specify the 'owner' (parent) of the association. Since I don't use fluent, I don't know how to specify that in your mapping, so I cannot give you an example.
Also, in Xml mapping, I'd use the 'property-ref' attribute to indicate that the 'specialIdentifier' is the column that is used by the relationship.
如果您想在 Fluent NHibernate 中进行这种映射,这里有一篇很好的文章:
http:// /wiki.fluentnhibernate.org/Fluent_mapping
If you are looking to do this kind of mapping in Fluent NHibernate, there is a good article on it here:
http://wiki.fluentnhibernate.org/Fluent_mapping