流畅的 Nhibernate 和 HasOne() 问题
如何与 Fluent nhibernate 建立 1 对 1 的关系?我正在使用 ms sql server 2008,每次我通过数据库图表查看器查看数据库表时,应该具有一对一关系的表似乎没有它们。
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>
{
Id(x => x.UserId);
HasOne(x => x.Setting);
}
public class SettingMap : ClassMap<Setting>
{
Id(x => x.UserId);
HasOne(x => x.User);
}
所以我尝试了这个但没有成功。
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>
{
Id(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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么你的Setting类中有一个Setting类型的属性?它被称为 User 所以我希望它(也是因为你对问题的描述)它是一个 User 类。
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: