views:

478

answers:

3

For a (very) long time I've been looking for an example on how to correctly implement a one-to-one mapping with Fluent NHibernate.

Most resources I find say

I think you mean a many-to-one

However no one actually gives an example on how to correctly implement the one-to-one relation.

So, could you give an one-to-one mapping example with Fluent NHibernate?

Note: I'm not interested in people saying "what's your model, you might actually need HasMany". No, thanks, I simply need a one-to-one example.

EDIT: To be more precise, I know the syntax. That's the only thing I could find by searching by myself. What I'm looking for is a more complete example, including a ((very) simple) database setup, and the whole mapping, of all entities that participate in the relationship, which I think would have reasonable size for Stack Overflow.

A: 

HasOne(x => x.Prop)

James Gregory
This is not an example, this is just the syntax. I'm looking for a some more contextual example, explaining the DB setup as well.
Bruno Reis
You said you didn't want a HasMany, that implied to me that you actually know what a one-to-one is to be able to choose it over something else.
James Gregory
@James Gregory, yes, I know what a one-to-one is, but I always run into problems with the mapping of IDs, autogenerated IDs.
Bruno Reis
Create your mappings, generate the schema, compare the results to how you normally do it.
James Gregory
+1  A: 

This is the best example I've seen. Hopefully it meets your needs.

Jamie Ide
+1  A: 

I've solved my problem.

I've also written a somewhat detailed article on this problem, that you can find at: http://brunoreis.com/tech/fluent-nhibernate-hasone-how-implement-one-to-one-relationship/

You will find a scenario in which we want a one-to-one relationship, the database schema as we would like it, the code of the model as it needs to be to meet NHibernate requirements, and the Fluent mapping that corresponds to the situation.

Bruno Reis