Fluent NHibernate Automappings 为 1 个关系生成 2 个外键

发布于 2024-12-06 08:37:19 字数 812 浏览 0 评论 0原文

我有这个设置(为简洁起见而精简)

Class Employee
    virtual IList<ChecklistItem> HasInitialed { get; private set; }

Class ChecklistItem
    virtual Employee InitialedBy { get; set; }

生成后,我得到这些表

Employee
    Id

ChecklistItem
    Id
    InitialedBy_id <----
    Employee_id <----

ChecklistItem 有 2 个 Employee 外键,我假设 Employee_id 映射 ChecklistItem.Employee 和 InitialedBy_id 映射 ChecklistItem.InitialedBy。我如何告诉 NHibernate 这是相同的双向关系并且只需要 1 个外键?

一般来说,我对 NHibernate 比较陌生,但这似乎应该是相当标准的。

这是我在配置数据库以生成正确的架构时想到的,对吗?

.Override<Employee>(map => map
    .HasMany<ChecklistItem>(x => x.HasInitialed)
    .KeyColumn("InitialedBy_id"))

如果这是正确的,有没有办法根据 ChecklistItem 的属性名称(lambda)选择 KeyColumn?

I have this setup (condensed for brevity)

Class Employee
    virtual IList<ChecklistItem> HasInitialed { get; private set; }

Class ChecklistItem
    virtual Employee InitialedBy { get; set; }

When this is generated I get these tables

Employee
    Id

ChecklistItem
    Id
    InitialedBy_id <----
    Employee_id <----

The ChecklistItem has 2 foreign keys for Employee, I'm assuming Employee_id to map ChecklistItem.Employee and InitialedBy_id to map ChecklistItem.InitialedBy. How can I tell NHibernate that this is the same bidirectional relationship and only requires 1 foreign key?

I'm kind of new to NHibernate in general, but this seems like it should be pretty standard.

This is what I've come up with when I'm configuring my database to generate the right schema, is it right?

.Override<Employee>(map => map
    .HasMany<ChecklistItem>(x => x.HasInitialed)
    .KeyColumn("InitialedBy_id"))

If that is right, is there a way to choose KeyColumn based on the ChecklistItem's property name (a lambda)?

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

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

发布评论

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

评论(1

音栖息无 2024-12-13 08:37:19

作为所有 hasmanies 的约定

class HasManyConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Key.Column(((ICollectionInspector)instance).Name + "_id");
    }
}

或仅作为

class HasManyConvention : IHasManyConvention, IHasManyConventionAcceptance
{
    public void Accept(IAcceptanceCriteria<IOneToManyCollectionInspector> criteria)
    {
        criteria.Expect(x => x.Name == "HasInitialed"); // and/or entity type
    }

    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Key.Column(((ICollectionInspector)instance).Name + "_id");
    }
}

Manytoone/References 的约定

class ReferenceConvention : IReferenceConvention
{
    public void Apply(IManyToOneInstance instance)
    {
        instance.Column(instance.Name + "_id");
    }
}

as a convention for all hasmanies

class HasManyConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Key.Column(((ICollectionInspector)instance).Name + "_id");
    }
}

or only for this

class HasManyConvention : IHasManyConvention, IHasManyConventionAcceptance
{
    public void Accept(IAcceptanceCriteria<IOneToManyCollectionInspector> criteria)
    {
        criteria.Expect(x => x.Name == "HasInitialed"); // and/or entity type
    }

    public void Apply(IOneToManyCollectionInstance instance)
    {
        instance.Key.Column(((ICollectionInspector)instance).Name + "_id");
    }
}

convention for Manytoone/References

class ReferenceConvention : IReferenceConvention
{
    public void Apply(IManyToOneInstance instance)
    {
        instance.Column(instance.Name + "_id");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文