流畅的 Nhibernate 和 HasOne() 问题

发布于 2024-10-13 20:14:23 字数 669 浏览 2 评论 0原文

如何与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千寻… 2024-10-20 20:14:23

为什么你的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.

七秒鱼° 2024-10-20 20:14:23

我遇到了类似的问题,看不到生成任何关系。我终于发现这有效:

 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();           
   }
}

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();           
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文