更改 FluentNhibernate 自动映射更改中的约束名称

发布于 2024-09-27 05:59:33 字数 543 浏览 2 评论 0原文

我正在使用 Oracle 和 FluentNHibernate 自动映射,并进行更改和修改。 NHibernate 问题是如何通过覆盖映射模型来指定约束名称?

生成的sql如下:

alter table FirstTable
    add constraint FK_VerLongIdentifierLongerThan30Characther
    foreign key (FirstTableID) 
    references SecondTable;

我需要通过覆盖映射模型将“FK_VerLongIdentifierLongerThan30Characther”更改为更小的标识符,如下所示:

model.Override<SomeClass>(m =>
    {
        m.HasOne<SomeOtherClass>(c => c.SomeProperty).?????????;
        //or
        m.????????
    }
  )

i'm using oracle with FluentNHibernate automapping with alterations & NHibernate
the problem is how to specify the constraint name by overriding the mapping model??

the generated sql like this:

alter table FirstTable
    add constraint FK_VerLongIdentifierLongerThan30Characther
    foreign key (FirstTableID) 
    references SecondTable;

i need to change the "FK_VerLongIdentifierLongerThan30Characther" to smaller identifier by overriding the mapping model like this:

model.Override<SomeClass>(m =>
    {
        m.HasOne<SomeOtherClass>(c => c.SomeProperty).?????????;
        //or
        m.????????
    }
  )

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

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

发布评论

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

评论(1

初与友歌 2024-10-04 05:59:33

我所做的不是对每个存在此问题的类进行覆盖,而是创建一个 映射约定 会截断外键上超过 30 个字符的命名方案的对象,具有许多多对多的关系:


public class OracleIHasManyConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        var keyName = string.Format(CultureInfo.InvariantCulture, "FK_{0}_{1}",
                                    instance.Member.Name,
                                    instance.EntityType.Name).Truncate(30);

        instance.Key.ForeignKey(keyName);
    }
}

public class OracleForeignKeyConvention : FluentNHibernate.Conventions.ForeignKeyConvention
{
    protected override string GetKeyName(Member property, System.Type type)
    {
        var name = property == null
                   ? "Id_" + type.Name.ToUnderscoredNaming()
                   : "Id_" + property.Name.ToUnderscoredNaming();

        return name.Truncate(30);
    }
}

然后我会这样调用这些约定:

var cfg = Fluently.Configure()
                .Database(SQLiteConfiguration.Standard.UsingFile("foobar.lite3"))
                .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Product>()
                                                      .Where(a => a.Namespace == typeof (Product).Namespace)
                                                      .Conventions.Add<OracleIHasManyConvention>()
                                                      .Conventions.Add<OracleForeignKeyConvention>()
                                                      .Conventions.Add<OracleGeneralConvention>()
                                                      .Conventions.Add<CascadeAllConvention>()
                                   ));


Here's the Truncate extension:

public static class StringHelper
{
    public static string Truncate(this string text, int endIndex)
    {
        if (text.Length > endIndex)
        {
            text = text.Substring(0, endIndex).TrimEnd('_');
        }
        return text;
    }
}

我希望这在任何方面都有用。 :)

Instead of doing an override for each class with this problem, what I have done is create a mapping convention which would truncate objects with surpass a 30-character naming scheme on foreign keys, has many and many to many relations:


public class OracleIHasManyConvention : IHasManyConvention
{
    public void Apply(IOneToManyCollectionInstance instance)
    {
        var keyName = string.Format(CultureInfo.InvariantCulture, "FK_{0}_{1}",
                                    instance.Member.Name,
                                    instance.EntityType.Name).Truncate(30);

        instance.Key.ForeignKey(keyName);
    }
}

public class OracleForeignKeyConvention : FluentNHibernate.Conventions.ForeignKeyConvention
{
    protected override string GetKeyName(Member property, System.Type type)
    {
        var name = property == null
                   ? "Id_" + type.Name.ToUnderscoredNaming()
                   : "Id_" + property.Name.ToUnderscoredNaming();

        return name.Truncate(30);
    }
}

Then I would call these conventions like this:

var cfg = Fluently.Configure()
                .Database(SQLiteConfiguration.Standard.UsingFile("foobar.lite3"))
                .Mappings(m => m.AutoMappings.Add(AutoMap.AssemblyOf<Product>()
                                                      .Where(a => a.Namespace == typeof (Product).Namespace)
                                                      .Conventions.Add<OracleIHasManyConvention>()
                                                      .Conventions.Add<OracleForeignKeyConvention>()
                                                      .Conventions.Add<OracleGeneralConvention>()
                                                      .Conventions.Add<CascadeAllConvention>()
                                   ));


Here's the Truncate extension:

public static class StringHelper
{
    public static string Truncate(this string text, int endIndex)
    {
        if (text.Length > endIndex)
        {
            text = text.Substring(0, endIndex).TrimEnd('_');
        }
        return text;
    }
}

I hope this is useful in any way. :)

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