fluent NHibernate one-to-one relationship?

2019-05-19 22:13发布

问题:

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.