Mapping OneToOne by nhibernate 3.2 code

I am trying to update a project and use the assembly in the encoder.

I have 2 objects:

public class Person { public virtual int Id { get; set; } public virtual User User { get; set; } } public class User { public virtual int Id { get; set; } public virtual User Person { get; set; } } 

The structure of the database is as follows:

 table: users, fields: id table: people, fields: id, userId 

With FluentNHibernate, I could map this as follows:

 public class UserMap : ClassMap<User> { public UserMap() { Id(x => x.Id).GeneratedBy.Identity(); HasOne(x => x.Person).PropertyRef("User").Not.LazyLoad(); } } public class PersonMap : ClassMap<Person> { public PersonMap() { Id(x => x.Id).GeneratedBy.Identity(); References(x => x.User).Column("UserId").Not.Nullable().Not.LazyLoad(); } } 

But you can’t get it to work with NH 3.2 in the code collector. This is what I have done so far.

 OneToOne(x=>x.Person, m => { m.PropertyReference(typeof(Person).GetProperty("User")); }); OneToOne(x=>x.User, m => {}); 

Now the relationship maps to User.Id and Person.Id, but personid and userid may be different. It is also possible that the user does not have a person.

 from Users user0_ left outer join People person1_ on user0_.Id=person1_.Id 

I think I need to indicate that Person β†’ User maps to the UserId column, but how ?.

+4
source share
1 answer

Since you used References() in Fluent, you need to convert it to ManyToOne() :

 public class UserMap : ClassMapping<User> { public UserMap() { Id(x => x.Id, x => x.Generator(Generators.Identity)); OneToOne(x => x.Person, x => x.PropertyReference(typeof(Person).GetProperty("User"))); } } public class PersonMap : ClassMapping<Person> { public PersonMap() { Id(x => x.Id, x => x.Generator(Generators.Identity)); ManyToOne(x => x.User, x => x => { x.Column("UserId"); x.NotNullable(true); }); } } 

Two notes:

+5
source

All Articles