Fluent Nhibernate 1.0 - 指定类和连接子类之间的外键约束名称

发布于 2024-09-07 02:14:33 字数 501 浏览 8 评论 0原文

我认为这应该很简单,但我不知道该怎么做。假设我有以下映射:

public class AnimalMap : ClassMap<Animal> { Id( x => x.Id); }

public class CatMap: SubclassMap<Cat> {
    Extends<AnimalMap>();
    Map(x => x.IsDomestic);
}

它按照我的预期创建表:

Animal
------
Id

Cat
----
AnimalId : FK to Animal (named FK3500ABA0D)
IsDomestic

如前所述,FK 由数据库生成,最终为 FK3500ABA0D。我想做的就是设置该约束的名称,但我找不到如何通过 Fluent NHibernate(或者实际上是普通的 NHibernate)来完成它。

那么,我错过了什么?

I think this should be simple, but I can't figure out how to do it. Suppose I have the following maps:

public class AnimalMap : ClassMap<Animal> { Id( x => x.Id); }

public class CatMap: SubclassMap<Cat> {
    Extends<AnimalMap>();
    Map(x => x.IsDomestic);
}

Which creates tables as I expect:

Animal
------
Id

Cat
----
AnimalId : FK to Animal (named FK3500ABA0D)
IsDomestic

As noted, the FK gets generated by the db and ends up as FK3500ABA0D. All I want to do is set the name of that constraint, but I can't find how to do it via Fluent NHibernate (or actually even plain NHibernate, for that matter).

So, what am I missing?

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

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

发布评论

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

评论(3

開玄 2024-09-14 02:14:33

Fluent NH 确实允许这样做:

public class ReferenceConvention : IReferenceConvention{
     public void Apply(IManyToOneInstance instance) {
          instance.ForeignKey(string.Format("FK_{0}_{1}",
               instance.EntityType.Name,
               instance.Name));
     }
}

您还需要以与上述相同的方式实现 IHasManyConventionIHasManyToManyConvention

Fluent NH does allow this:

public class ReferenceConvention : IReferenceConvention{
     public void Apply(IManyToOneInstance instance) {
          instance.ForeignKey(string.Format("FK_{0}_{1}",
               instance.EntityType.Name,
               instance.Name));
     }
}

You'd also need to implement IHasManyConvention and IHasManyToManyConvention in the same way as above.

〗斷ホ乔殘χμё〖 2024-09-14 02:14:33

我不知道 FluentNH 是否支持,但是 XML 很简单:

<joined-subclass name="Cat">
  <key column="AnimalId" foreign-key="NameOfTheFK"/>
</joined-subclass>

I don't know if FluentNH supports it, but the XML is simple:

<joined-subclass name="Cat">
  <key column="AnimalId" foreign-key="NameOfTheFK"/>
</joined-subclass>
橘虞初梦 2024-09-14 02:14:33

我遇到了同样的问题,以下内容对我来说效果很好:

public class JoinedSubclassForeignKeyConvention : IJoinedSubclassConvention
{
    public void Apply(IJoinedSubclassInstance instance)
    {
        if (instance.Type.BaseType != null)
            instance.Key.ForeignKey(string.Format("FK_{0}_{1}", instance.EntityType.Name, instance.Type.BaseType.Name));
    }
}

然后您的外键约束将被命名为 FK_Cat_Animal

I had the same problem, the following works well for me:

public class JoinedSubclassForeignKeyConvention : IJoinedSubclassConvention
{
    public void Apply(IJoinedSubclassInstance instance)
    {
        if (instance.Type.BaseType != null)
            instance.Key.ForeignKey(string.Format("FK_{0}_{1}", instance.EntityType.Name, instance.Type.BaseType.Name));
    }
}

Your foreign key constraint would then be named as FK_Cat_Animal

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