Ihar Krasnik
Ihar Krasnik

Reputation: 2589

Fluent NHibernate One-To-One with only one foreign column

I have 2 one-to-one tables in my db, and only one of them has foreign key to another table. Here is an example of what I have

Table 1: Container
- Id (PK)

Table 2: Item
- Id (PK)
- ContainerId (FK)

Can I configure their relations as one to one to have following models mapped:

public class Container{
     public virtual int Id {get;set;}
     public virtual Item Item {get;set;}
}

public class Item{
     public virtual int Id {get;set;}
     public virtual Container {get;set;}
}

I can configure Item to have one Container, because it has explicit column for this, but how it can be done for Container?

Thanks!

Upvotes: 1

Views: 1300

Answers (1)

Cole W
Cole W

Reputation: 15293

That is not a true one to one relationship. Both primary keys must be the same in both tables for it to be a one to one. Container.Id != Item.Id. That is a many-to-one.

In your item mapping you would have something like this:

Id(x => x.Id);
References(x => x.Container);

Your Container mapping would be similar.

http://www.jagregory.com/writings/i-think-you-mean-a-many-to-one-sir/

Upvotes: 1

Related Questions