NHibernate 父子映射

发布于 2024-12-06 18:50:45 字数 696 浏览 0 评论 0原文

我在表 ACCOUNT 和表 USER 之间有父/子数据库关系。目前,我已经映射了如下所示的双向 Fluent 映射:

public class Account {
  public virtual IList<User> ListUsers { get; private set; }

  public virtual void AddUser(User user)
  {
    user.Account = this;
    ListUsers.Add(user);
  }
}

MAPPING: HasMany(x => x.ListUsers).Table("UserInfo").KeyColumn("Id_Account").Inverse().Cascade.All().AsList();

public class User {
  public virtual Account Account { get; set; }
  public string Username { get; set; } 
}

MAPPING: References(x => x.Account).Column("Id_Account");

在实践中,我无法预见我会想要从用户实体引用帐户实体。同样,我无法预见我想要从父帐户实体加载所有用户实体。我对 NHibernate 相当陌生,想知道上述方法是否仍然是提高性能的最佳方法?双向关系是首选吗?我应该只引用 Id 吗?谢谢

I have a parent/child database relationship between the table ACCOUNT and the table USER. Currently I have mapped a bi-directional Fluent mappings like this:

public class Account {
  public virtual IList<User> ListUsers { get; private set; }

  public virtual void AddUser(User user)
  {
    user.Account = this;
    ListUsers.Add(user);
  }
}

MAPPING: HasMany(x => x.ListUsers).Table("UserInfo").KeyColumn("Id_Account").Inverse().Cascade.All().AsList();

public class User {
  public virtual Account Account { get; set; }
  public string Username { get; set; } 
}

MAPPING: References(x => x.Account).Column("Id_Account");

In practice I cannot foresee that I will ever want to reference the Account entity from the User entity. Likewise I cannot foresee my wanting to load all of the User entities from the parent Accounts entity. I am fairly new to NHibernate and was wondering is the above method still the best way to go performance wise? Is a bi-directional relationship preferred and should I look to referencing the Id only? Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

遥远的她 2024-12-13 18:50:45
  1. 在我看来,双向引用正确的方法。如果您在 Account 属性上使用延迟加载,那么它只会加载帐户的 ID。
  2. 您指定 ListUsers 属性为 inverse=true,这意味着 User 实体负责保存引用。因此,我相信(如果我没记错的话)ListUsers.Add(user); 行不是必需的,因为关联将由 User 实体创建。< br>
    因此,这意味着您在添加用户时不必从数据库加载整个 ListUsers 集合。
  1. bi-directional references is the correct approach, in my opinion. If you use lazy-loading on the Account property, then it would only load the account's id anyway.
  2. you specified that the ListUsers property is inverse=true, meaning the User entity is responsible for saving the reference. Therefore, I believe (if I remember correctly) that the line ListUsers.Add(user); is not necessary, since the association will be created by the User entity.
    So this means that you don't have to load the entire ListUsers collection from the db when you add a user.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文