将表继承映射为与 NHibernate 的一对一关系
我有一个数据库,它在人员和地址(使用人员 ID)之间建立了一对一的关系。但是,我找不到使用 NHibernate 制作地图的方法。
我的表结构如下:
PersonTable PersonId PersonName PersonAge AddressTable PersonId CountryName StreetName StateName
我希望有这样的东西作为最终类:
PersonClass int Id string Name int Age Address HomeAddress AddressClass string Street string Country string State Person Owner
我尝试使用 HasOne 关系,但无法重用 PersonId 作为地址标识符。
谢谢!
编辑:我忘了提及我正在使用 FluentNHibernate,因此流畅的映射和 XML 都可以。
I have a database that has a one-to-one relationship modeled between a Person and a Address (that uses person id). However, I cannot find a way to make the map using NHibernate.
My table structure is the following:
PersonTable PersonId PersonName PersonAge AddressTable PersonId CountryName StreetName StateName
And I would like to have something like this as the final class:
PersonClass int Id string Name int Age Address HomeAddress AddressClass string Street string Country string State Person Owner
I tried with HasOne relationship but I couldn´t reuse PersonId as Address Identifier.
Thanks!
Edit: I forgot to mention that I´m using FluentNHibernate so both fluent mapping and XML will be fine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于您的数据库架构并不表示人员和地址之间的“Has One”关系。它代表一种“Has Many”关系;您可能人为地将其限制为每人一个地址,但这并不能改变模型是每人多个地址的事实。
要获得“Has One”关系,您可以将 AddressID 放在 PersonTable 上。
The problem is that your database schema does not represent a "Has One" relationship between Person and Address. It represents a "Has Many" relationship; You may be artificially limiting it to be one address per person, but that doesn't change the fact that the model is multiple addresses per person.
To get a "Has One" relationship, you would put the AddressID on the the PersonTable.
我在 Address 类映射中使用 Id().GenerateBy.Foreign() 来完成此操作,将其引用到 Person 的 ID 并且它有效。
谢谢!
I did it using Id().GeneratedBy.Foreign() in Address class map referencing it to the Person´s ID and it worked.
Thanks!
我会将其映射为 Person 的一个组件。在 person ClassMap 中添加以下内容:
干杯
I would map it as a component of Person. In the person ClassMap add the following:
Cheers