Fluent NHibernate:如何在两个方向上进行一对多引用?
我们的系统上有用户和人员。每个用户有很多人。但是当用户登录时,我们需要查找他的主要 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
假设您的 ID 是自动编号,并且当您更新用户时,您还希望您的 PrimaryPerson 也随之更新。如果不是这种情况,请将 Cascade.All() 更改为 Cascade.None() 。请确保在执行此操作时手动更新 PrimaryPerson,否则如果不这样做,您将在 SubmitChanges() 上收到“对象引用未保存的瞬态实例”。
Try this:
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.
根据我对您的问题的评论,我认为映射将类似于:
编辑:添加表结构。
Based on my comment on your question, I think the mapping would be something along the lines of:
Edit: Added Table Structure.