流畅的NHibernate一对一关系

时间:2012-03-29 07:39:46

标签: nhibernate fluent-nhibernate

我有表地址和许多帐户表。 如何为这种情况编写Fluent映射一对一关系?

+----------+
| Address  |
+----------+
| Id       | PK
+----------+

+----------+
| AccountA |
+---------+
| Id       | PK
| AddressId| FK
+----------+

+----------+
| AccountB |
+---------+
| Id       | PK
| AddressId| FK
+----------+

1 个答案:

答案 0 :(得分:1)

这是自动映射。

.Override<Address>(mapping => mapping.HasOne(address => caddress.AddressA).Cascade.All().LazyLoad())
.Override<Address>(mapping => mapping.HasOne(address => caddress.AddressB).Cascade.All().LazyLoad())
.Override<AddressA>(mapping => mapping.Id(x => x.Id).GeneratedBy.Foreign("Address"))
.Override<AddressB>(mapping => mapping.Id(x => x.Id).GeneratedBy.Foreign("Address"))
.Override<AddressA>(
     mapping => mapping.HasOne(
    Reveal.Member<AddressA, Address>("Address")).Constrained().ForeignKey().LazyLoad())
.Override<AddressB>(
     mapping => mapping.HasOne(
    Reveal.Member<AddressB, Address>("Address")).Constrained().ForeignKey().LazyLoad())

所有这些表都有相同的密钥:Id

+----------+
| Address  |
+----------+
| Id       | PK
+----------+

+----------+
| AccountA |
+---------+
| Id       | PK
+----------+

+----------+
| AccountB |
+---------+
| Id       | PK
+----------+