Fluent NHibernate:如何在两个方向上进行一对多引用?

发布于 2024-10-08 05:57:33 字数 817 浏览 3 评论 0原文

我们的系统上有用户人员。每个用户有很多人。但是当用户登录时,我们需要查找他的主要 Person 记录来填写他的姓名、地址、电话等。

public class Person
{
    /// <summary>Every Person belongs to a user.</summary>
    public virtual User User { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Address { get; set; }
    public virtual string Phone { get; set; }
    // More methods & Properties
}

public class User : Entity
{
    public virtual string Username { get; set; }
    public virtual string Password { get; set; }
    /// <summary>Every User has a primary person record.</summary>
    public virtual Person PrimaryPerson { get; set; }
    // More methods & Properties
}

类映射如何在没有循环引用的情况下查找这些记录?

We have Users on our system and Persons. There are many Persons for each User. But when a user logs on, we need to look up his primary Person record to fill in his name, address, phone, etc.

public class Person
{
    /// <summary>Every Person belongs to a user.</summary>
    public virtual User User { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Address { get; set; }
    public virtual string Phone { get; set; }
    // More methods & Properties
}

public class User : Entity
{
    public virtual string Username { get; set; }
    public virtual string Password { get; set; }
    /// <summary>Every User has a primary person record.</summary>
    public virtual Person PrimaryPerson { get; set; }
    // More methods & Properties
}

How does the class map look for this without a circular reference?

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

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

发布评论

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

评论(2

猛虎独行 2024-10-15 05:57:33

试试这个:

public class PersonMap : ClassMap<Person>
{
  public PersonMap()
  {
    Table("Persons");
    Id(x =>x.Id, "PersonId").GeneratedBy.Identity();
    References(x => x.User).Column("UserId").Cascade.All();
    Map(x => x.FirstName, "FirstName");
    Map(x => x.LastName, "LastName");
    Map(x => x.Address, "Address");
    Map(x => x.Phone, "Phone");
    // More property maps
  }
}

public class UserMap : ClassMap<User>
{
  public UserMap()
  {
    Id(x => x.Id, "UserId").GeneratedBy.Identity();
    Map(x => x.Username, "Username");
    Map(x => x.Password, "Password");
    References<Person>(x => x.PrimaryPerson).ForeignKey("PrimaryPersonId").Cascade.All();
  }
}

假设您的 ID 是自动编号,并且当您更新用户时,您还希望您的 PrimaryPerson 也随之更新。如果不是这种情况,请将 Cascade.All() 更改为 Cascade.None() 。请确保在执行此操作时手动更新 PrimaryPerson,否则如果不这样做,您将在 SubmitChanges() 上收到“对象引用未保存的瞬态实例”。

Try this:

public class PersonMap : ClassMap<Person>
{
  public PersonMap()
  {
    Table("Persons");
    Id(x =>x.Id, "PersonId").GeneratedBy.Identity();
    References(x => x.User).Column("UserId").Cascade.All();
    Map(x => x.FirstName, "FirstName");
    Map(x => x.LastName, "LastName");
    Map(x => x.Address, "Address");
    Map(x => x.Phone, "Phone");
    // More property maps
  }
}

public class UserMap : ClassMap<User>
{
  public UserMap()
  {
    Id(x => x.Id, "UserId").GeneratedBy.Identity();
    Map(x => x.Username, "Username");
    Map(x => x.Password, "Password");
    References<Person>(x => x.PrimaryPerson).ForeignKey("PrimaryPersonId").Cascade.All();
  }
}

This is assuming your Ids are autonumber and that when you update a User, you also want your PrimaryPerson to be updated along with it. Change the Cascade.All() to Cascade.None() if that isn't the case. Just be sure that when you do, you update the PrimaryPerson manually or you'll get a "object references an unsaved transient instance" upon SubmitChanges() if you don't.

风轻花落早 2024-10-15 05:57:33

根据我对您的问题的评论,我认为映射将类似于:

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.Id)...
        ...
        References(x => x.User).Inverse(); // User references Person
    }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id)...
        ...
        References(x => x.PrimaryPerson).Column("PrimaryPersonId")...
        HasMany(x => x.People).KeyColumn("UserId").Inverse(); // UserId is on the Person
    }
}

编辑:添加表结构。


[User]
UserId
... -- Other Columns
PrimaryPersonId  -- FK to PersonId in Person table

[Person]
PersonId
... -- Other Columns
UserId -- FK to UserId in User Table for user has many Persons. 

Based on my comment on your question, I think the mapping would be something along the lines of:

public class PersonMap : ClassMap<Person>
{
    public PersonMap()
    {
        Id(x => x.Id)...
        ...
        References(x => x.User).Inverse(); // User references Person
    }
}

public class UserMap : ClassMap<User>
{
    public UserMap()
    {
        Id(x => x.Id)...
        ...
        References(x => x.PrimaryPerson).Column("PrimaryPersonId")...
        HasMany(x => x.People).KeyColumn("UserId").Inverse(); // UserId is on the Person
    }
}

Edit: Added Table Structure.


[User]
UserId
... -- Other Columns
PrimaryPersonId  -- FK to PersonId in Person table

[Person]
PersonId
... -- Other Columns
UserId -- FK to UserId in User Table for user has many Persons. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文