I am trying to figure out a way to map one to one relationship in nhibernate when referencing column is not a primary key column on second table.
For example consider
Person Table
PersonId (pk)
Name
and
Passport Table
PassportId (pk)
Country
PersonId
The two tables have one to one relationship on PersonId.
My Nhibernate Model looks like below.
public class Person
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Passport Passport { get; set; }
}
public class Passport
{
public virtual int Id { get; set; }
public virtual string Country { get; set; }
public virtual Person Person { get; set; }
}
Based on explanation form this article I defined the relationship mapping as follows but it didn’t work
PersonMapping:
OneToOne(x => x.Passport, x => x.Cascade(Cascade.All));
PassportMapping:
ManyToOne(x => x.Person, x => { x.Unique(true); x.Column("PersonId");});
it is constructing the sql query as follows
select * from Person
left outer join Passport on Persson.PersonId = Passport.PassportId.
it assumes that the PassportId and PersonId has same value but in my case they are different. How can I define my mapping in such case using mapping by code.