【问题标题】:FluentNHibernate one-to-one MappingFluentNHibernate 一对一映射
【发布时间】:2012-01-21 01:08:56
【问题描述】:
我有 3 张桌子:
表:公司,列:ID、名称
表:用户,列:Id、Name、CompanyId
表:CompanyOwnerInfo,列:Id、CompanyId、OwnerName.....
User 和
CompanyOwnerInfo 表中的
CompanyId 列都映射到 Company.Id
我的问题是如何在不涉及 Company 表的情况下为 User 和 CompanyOwnerInfo 表创建一对一映射?因为当我使用 User 对象来获取 CompanyOwnerInfo 时,没有必要加入 Company 表?
【问题讨论】:
标签:
nhibernate
fluent-nhibernate
mapping
one-to-one
【解决方案1】:
public UserMap()
{
References(x => x.Company, "CompanyId");
References(x => x.CompanyOwnerInfo, "CompanyId")
.PropertyRef(compOwner => compOwner.Company)
.Fetch.Join()
.Not.LazyLoad() // if needed
.ReadOnly(); // important otherwise you have Exception on save because both References want to write to the same column
}