fluent NHibernate one-to-one relationship?

对着背影说爱祢 提交于 2019-12-07 21:06:00

问题


I have a problem with one-to-one relationships in the fluent nHibernate.

I have the following relational table from the AdventureWorks2008 database.

BusinessEntity (Table)
    BusinessEntityId Int (PK, Identity)

Person (Table)
   BusinessEntityId int (PK, Reference with BusinessEntity table)
   FullName varchar(255)

The relationship between BusinessEntity table and Person table is one-to-one.

How do I map fluently without any extra field like "Id" in the Person table?

There should be 2 class one for Person and another for BusinessEntity, or an appropriate model to best describe the above relation.

Thanks, Ashraf.


回答1:


presuming your Person mapping is pretty standard, the way you do this is by saying:

Id(x => x.BusinessEntityId)
     .GeneratedBy.Foreign("BusinessEntity");

on the Person class.

This presumes that your Person class has a property called BusinessEntity which is of type BusinessEntity.

You'll also need to map BusinessEntity to Person with constrained set to true (to say that they primary key of Person is a foreign key reference to BusinessEntity).

The key thing is the GeneratedBy.Foreign() to say that your identity is generated by a link to another class.



来源:https://stackoverflow.com/questions/1597979/fluent-nhibernate-one-to-one-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!