FluentNHibernate:同一实体之间的多个一对多关系

发布于 2024-08-31 15:05:07 字数 1008 浏览 4 评论 0原文

我正在开发一个错误跟踪应用程序。有票证,每张票证都有一个开启者用户和一个指定用户。所以,基本上,我有两个实体,它们彼此之间有两个多对一的关系。他们的原理图是这样的:

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

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

发布评论

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

评论(1

就是爱搞怪 2024-09-07 15:05:09

自动映射为其外键列生成相同的名称。您可以通过自定义约定来克服这个问题,通过(映射的)属性名称命名外键列:

class HasManyConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Key.Column(instance.Member.Name);
    }
}

// register convention using:
autoMapping.Conventions.Add(new HasManyConvention());

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:

class HasManyConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Key.Column(instance.Member.Name);
    }
}

// register convention using:
autoMapping.Conventions.Add(new HasManyConvention());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文