将表继承映射为与 NHibernate 的一对一关系

发布于 2024-08-22 22:15:23 字数 506 浏览 3 评论 0原文

我有一个数据库,它在人员和地址(使用人员 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 技术交流群。

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

发布评论

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

评论(3

雪化雨蝶 2024-08-29 22:15:23

问题在于您的数据库架构并不表示人员和地址之间的“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.

花开半夏魅人心 2024-08-29 22:15:23

我在 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!

︶葆Ⅱㄣ 2024-08-29 22:15:23

我会将其映射为 Person 的一个组件。在 person ClassMap 中添加以下内容:

 Component(x => x.Address, m =>
        {
            m.Map(x => x.Street, "Street");
            m.Map(x => x.State, "State");
            // more here
        });

干杯

I would map it as a component of Person. In the person ClassMap add the following:

 Component(x => x.Address, m =>
        {
            m.Map(x => x.Street, "Street");
            m.Map(x => x.State, "State");
            // more here
        });

Cheers

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