如何阻止 Fluent NHibernate 创建外键

发布于 2024-09-15 02:22:16 字数 1378 浏览 4 评论 0原文

我得到了如下例所示的多态关系:

public class A
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
}

Class B & C 包含 A 列表:

public class B/C
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<A> As { get; set; }
    public virtual SomeParent Parent { get; set; }
}

我的目标是像“

session.Linq<B>().Where(x => x.Parent == someParent && x.As.Contains(someA));

当前我在 B => 之间配置了多对多关系”这样的查询。 A和C=> A 使用共享链接表,因为我希望将所有链接放在一个表中。 在此示例中,NH shema 导出创建 4 个表:A、B、C 和 ChildToA。

HasManyToMany(x => x.As)
            .AsBag()
            .Table("XX_ChildToA")
            .ParentKeyColumn("Child_ID")
            .ChildKeyColumn("A_ID")
            .Cascade.All()

只要您只使用一个子类型,这种方法就可以正常工作,因为 shema 导出会生成一个外键,将“Child_ID”限制为导出时首先命中的任何表的 ID(在本例中为 B)。

var b = session.Get<B>(id);
b.As.Add(someA);
tx.Commit(); // works fine

var c = session.Get<C>(id);
c.As.Add(someA);
tx.Commit(); // crashes because of FK constraint

我可以阻止 FluentNHibernate 创建此 FK 吗?当我在 google 上搜索这个问题时,我注意到 HBM 样本在多对一关系中具有foreign-key="no" 属性。那么 NHibernate 应该能够解决这个问题,对吗?不过,我想保留我的流畅映射,因为我可以通过这种方式为所有子类型创建通用基映射类,并且目前我们所有的映射都是 FNH 映射。

I got a polymorphic relationship like the following example:

public class A
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
}

Class B & C contining a List of A's:

public class B/C
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<A> As { get; set; }
    public virtual SomeParent Parent { get; set; }
}

My goal are queries like

session.Linq<B>().Where(x => x.Parent == someParent && x.As.Contains(someA));

Currently I configured a Many-To-Many relation between B => A and C => A using a shared link table because I want to have all my links in one table.
In this examle NH shema export creates 4 tables, A, B, C and ChildToA.

HasManyToMany(x => x.As)
            .AsBag()
            .Table("XX_ChildToA")
            .ParentKeyColumn("Child_ID")
            .ChildKeyColumn("A_ID")
            .Cascade.All()

This works fine as long as you use only one the child types because shema export generates a foreign key restricting the "Child_ID" to IDs of whatever table it hits first while exporting (B in this case).

var b = session.Get<B>(id);
b.As.Add(someA);
tx.Commit(); // works fine

var c = session.Get<C>(id);
c.As.Add(someA);
tx.Commit(); // crashes because of FK constraint

Can I stop FluentNHibernate from creating this FK? While I searched google for this problem I noticed HBM samples with foreign-key="no" attributes in many-to-one relationships. So NHibernate should be able to solve this, right? However I would like to keep my fluent mappings because I can create a generic base mapping class for all my child types this way and currently all our mappings are FNH mappings.

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

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

发布评论

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

评论(2

水染的天色ゝ 2024-09-22 02:22:16

这应该可以做到:

HasManyToMany(x => x.As)
  .ForeignKeyConstraintNames("no", "no");

This should do it:

HasManyToMany(x => x.As)
  .ForeignKeyConstraintNames("no", "no");
莫多说 2024-09-22 02:22:16

我并不完全熟悉 FluentNHibernate,但我假设您可以使用以下内容在集合的映射上设置此自定义属性:

.SetAttribute("foreign-key", "no")

I'm not entirely familiar with FluentNHibernate, but I'm assuming you could set this custom attribute on your mapping for the collection using something like:

.SetAttribute("foreign-key", "no")

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