Fluent NHibernate Uni-Directional one to one mapping
I cant get at One-to-One relationship working with Fluent NHibernate. I have User and UserDetails tables and they 'share' a primary key. How do I map them?
This is my latest/best attempt, this fails with NHibernate.Id.IdentifierGenerationException: attempted to assign id from null one-to-one property: User
User
protected UserMap()
{
Table("user");
Id(x => x.Id)
.Column("user_key")
.GeneratedBy.GuidComb().UnsavedValue(Guid.Empty);
References(x => x.UserDetail)
.PropertyRef(x=>x.User)
.Column("user_key")
.Not.Insert().Not.Update().Cascade.All();
}
UserDetail
protected UserDetailMap()
{
Table("user_detail");
Id(x => x.Id).Column("user_key")
.GeneratedBy.Foreign("User")
.UnsavedValue(Guid.Empty);
References(x => x.User开发者_如何学运维)
.Column("user_key")
.Not.Insert().Not.Update().Unique().Cascade.None();
}
Try this
protected UserDetailMap()
{
Table("user_detail");
Id(x => x.Id)
.Column("user_key")
.GeneratedBy.Foreign("User")
.UnsavedValue(Guid.Empty);
HasOne(x => x.User)
.Constrained();
}
精彩评论