Fluent Nhibernate and HasOne() problems
How do you do a 1 to 1 relationship with fluent nhibernate? I am using ms sql server 2008 and every time I looked the db tables through the database diagram viewer the table that should have one to one relationships don't seem to have them.
Users
UserId <pk> Guid
Settings
UserId <pk> Guid
public Settings
{
public virtual Guid UserId {get; private set;}
public virtual Setting User { get; set; }
}
public User
{
public virtual Guid UserId {get; private set;}
public virtual Setting Setting { get; set; }
}
public class UserMap : ClassMap<User>
{
I开发者_C百科d(x => x.UserId);
HasOne(x => x.Setting);
}
public class SettingMap : ClassMap<Setting>
{
Id(x => x.UserId);
HasOne(x => x.User);
}
So I tried this but it did not work.
Why do you have a property of type Setting on your Setting class? It's called User so I would expect it (also because of your description of the problem) that it be a User class instead.
I had a similar problem, couldnt see any relationships being generated. I finally found that this worked:
public class ParentMap : ClassMap<Parent>
{
public ParentMap()
{
Id(x => x.Id);
HasOne(s => s.Child).Cascade.All();
}
}
public class ChildMap : ClassMap<Model.Child>
{
public ChildMap()
{
Id(x => x.Id);
HasOne(s => s.Parent).Constrained().ForeignKey();
}
}
精彩评论