2

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.

2
  • How are your model classes setup? Commented Oct 20, 2009 at 23:52
  • There should be 2 class one for Person and another for BusinessEntity. or an appropriate model. example public class Person { public virtual BusinessEntity BusinessEntity { get; set; } public virtual string Description { get; set; } } Commented Oct 21, 2009 at 0:06

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.