FluentNHibernate:同一实体之间的多个一对多关系
我正在开发一个错误跟踪应用程序。有票证,每张票证都有一个开启者用户和一个指定用户。所以,基本上,我有两个实体,它们彼此之间有两个多对一的关系。他们的原理图是这样的:
User:
public class User
{
public virtual int Id { get; protected set; }
...
public virtual IList<Ticket> OpenedTickets { get; set; }
public virtual IList<Ticket> AssignedTickets { get; set; }
}
Ticket:
public class Ticket
{
public virtual int Id { get; protected set; }
...
[Required]
public virtual User OpenerUser { get; set; }
public virtual User AssignedUser { get; set; }
}
我使用 FluentNHibernate 的自动映射功能。
问题是,无论我是否设置关系,在用户方面,两个集合始终包含相同的数据。我猜 Fluent 无法分辨哪个关系的哪一端属于哪里。
我用谷歌搜索但没有发现任何有用的东西。
编辑 2:
在 Steves 的帮助下,我发现我必须实现自己的 IHasManyConvention。我还发现 IHasManyConvention 中生成的列名称必须与 IReferenceConvention 生成的列名称匹配。经过一点调整,我让它工作了。
我还令人震惊地发现,调用方便的 ForeignKey.EndsWith("Id")
方法会搞乱整个事情。
虽然史蒂夫的回答本身并不能解决问题,但我非常感谢他为我指明了正确的方向。
I'm working on a bug tracking application. There are tickets, and each ticket has an opener user and an assigned user. So, basically, I have two entities, which have two many-to-one relationships with each other. Their schematic is this:
User:
public class User
{
public virtual int Id { get; protected set; }
...
public virtual IList<Ticket> OpenedTickets { get; set; }
public virtual IList<Ticket> AssignedTickets { get; set; }
}
Ticket:
public class Ticket
{
public virtual int Id { get; protected set; }
...
[Required]
public virtual User OpenerUser { get; set; }
public virtual User AssignedUser { get; set; }
}
I use FluentNHibernate's auto mapping feature.
The problem is, that no matter whether relationship I set, on the side of the User, both collections always contain the same data. I guess Fluent can't tell which end of which relationship belongs to where.
I googled around but haven't found anything useful.
EDIT 2:
I fould out with the help of Steves that I have to implement my own IHasManyConvention. I also found out that the generated column name in the IHasManyConvention has to match the column name generated by my IReferenceConvention. With a litte tweaking, I got it to work.
I also shockingly found that calling the convenient ForeignKey.EndsWith("Id")
method messes up the whole thing.
While Steves's answer in itself doesn't solve the problem, I am very thankful for him pointing me at the right direction.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
自动映射为其外键列生成相同的名称。您可以通过自定义约定来克服这个问题,通过(映射的)属性名称命名外键列:
Auto mappping generates the same name for their foreign key columns. You can overcome this with custom convention that names the foreign key column by the (mapped) property name: