如何编写使用序列的自动映射约定

发布于 2024-11-06 10:00:29 字数 1251 浏览 0 评论 0原文

我正在将一些映射从流畅映射范例更新为自动映射范例。我有一个名为 Group 的类,其中标识列的当前流畅映射如下所示:

Id(x => x.Id, "ID")
    .GeneratedBy
    .Native("GROUPS_SEQ");

导致 HBM 如下所示:

<id name="Id" type="System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <column name="ID" />
  <generator class="native">
    <param name="sequence">GROUPS_SEQ</param>
  </generator>
</id>

这非常适合我们的 Oracle 数据库以及使用 SQLite 的测试。不幸的是,我不知道如何创建一个 IIdConvention 约定来给我相同的结果。我已经到目前为止:

public class PrimaryKeyConvention : IIdConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
    {
        string pluralized = Inflector.Net.Inflector.Pluralize(instance.EntityType.Name);
        string underscored = Inflector.Net.Inflector.Underscore(pluralized);
        string uppercased = underscored.ToUpper();

        string sequenceName = string.Format("{0}_SEQ", uppercased);

        instance.Column("Id");
        instance.UnsavedValue("0");
        instance.GeneratedBy.Native(sequenceName);
    }
}

不幸的是,最后一行导致编译错误,因为 .Native 方法不接受序列名称字符串,就像在流畅映射中那样。有人对如何最好地解决这个问题有任何建议吗?

谢谢!

I'm with updating some of our mappings from the fluent mapping paradigm to the auto mapping paradigm. I have a class named Group where the current fluent mapping for the identity column looks like this:

Id(x => x.Id, "ID")
    .GeneratedBy
    .Native("GROUPS_SEQ");

Resulting in HBM that looks like this:

<id name="Id" type="System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <column name="ID" />
  <generator class="native">
    <param name="sequence">GROUPS_SEQ</param>
  </generator>
</id>

This works great with our Oracle database AND with tests that use SQLite. Unfortunately I can't figure out how to create an IIdConvention convention that will give me the same result. I've gotten this far:

public class PrimaryKeyConvention : IIdConvention
{
    public void Apply(FluentNHibernate.Conventions.Instances.IIdentityInstance instance)
    {
        string pluralized = Inflector.Net.Inflector.Pluralize(instance.EntityType.Name);
        string underscored = Inflector.Net.Inflector.Underscore(pluralized);
        string uppercased = underscored.ToUpper();

        string sequenceName = string.Format("{0}_SEQ", uppercased);

        instance.Column("Id");
        instance.UnsavedValue("0");
        instance.GeneratedBy.Native(sequenceName);
    }
}

Unfortunately the last line causes a compile error because the .Native method doesn't accept a sequence name string like it does in the fluent mappings. Does anyone have any suggestions on how best to solve this?

Thanks!

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

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

发布评论

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

评论(1

2024-11-13 10:00:29

GeneratorInstance.cs 文件已包含以下方法的实现:

void Native(string sequenceName);
void Native(string sequenceName, Action<ParamBuilder> paramValues);

这些方法未在 IGeneratorInstance.cs 接口中公开。我继续添加它们,这使我能够创建我需要的主键约定。据我所知,它们工作得很好。不幸的是,我公司的 IT 安全小组认为关闭防火墙上的一堆端口是合适的,这阻止了我通过 github 回馈这些端口。因此,如果有人愿意为我提交更新,我将不胜感激。

谢谢!

The GeneratorInstance.cs file already contains implementations for the following methods:

void Native(string sequenceName);
void Native(string sequenceName, Action<ParamBuilder> paramValues);

The methods are not exposed in the IGeneratorInstance.cs interface. I went ahead and added them which allowed me to create the primary key convention that I need. As near as I can tell they work fine. Unfortunately my company's IT security group has seen fit to close a bunch of ports on their firewall which prevents me from contributing this back via github. So if anyone wants to submit the update for me I would appreciate it.

Thanks!

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