让 NHibernate 生成 HiLo 字符串 ID

发布于 2024-08-09 21:23:04 字数 986 浏览 3 评论 0原文

我们有一个大型系统,通过 Unity 与其数据源 (Navision) 松散绑定 - 我们有机会将其换出并拥有自己的数据库。

因此,我们环顾四周,非常喜欢 Fluent NHibernate 的外观 - 我们正在尝试进行概念验证并更换一些服务。

我们想要使用 NHibernates HiLo 算法 - 不幸的是,我们从 Navision 继承了字符串 ID,它为其 ID 加上前缀(例如 COL00001),因此为了匹配接口,我们需要使用字符串 ID。

有谁知道我如何得到类似...

Id(x => x.ID).GeneratedBy.HiLo("100");

在 ID 是字符串的情况下工作?我们目前正在获取 Identity 必须是 int、long 等

谢谢,

Andy

------ 更新 ------

我尝试了建议的文章中的示例,但此功能已从 Fluent NHibernate 的更高版本中删除 - 那里然而是一个 .Custom - 但我似乎无法让它工作!

public class ManufacturerMap : ClassMap<Manufacturer>
{
    public ManufacturerMap()
    {
        Id(x => x.ID).GeneratedBy.Custom(typeof(StringTableHiLoGenerator));
        Map(x => x.Name);
    }
}


public class StringTableHiLoGenerator : TableHiLoGenerator
{
    public override object Generate(ISessionImplementor session, object obj)
    {
        return base.Generate(session, obj).ToString();
    }
}

We've got a large system that's loosely bound to its data source (Navision) via Unity - we're getting the opportunity to swap it out and have our own database.

So we've had a look around and really like the look of Fluent NHibernate - we're trying to get a proof of concept going and swap out a couple of the services.

We want to use NHibernates HiLo algorithm - Unfortunately we've inherited string ID's from Navision which prefixs its ID's (example COL00001) so to match the Interface we need to use string Id's.

Does anyone know how I'd get something like ...

Id(x => x.ID).GeneratedBy.HiLo("100");

working where ID is a string? We're currently getting Identity must be int, long etc

Thanks,

Andy

------ Update ------

I tried the example in the article suggested but this functionality has been removed from later versions of Fluent NHibernate - there is however a .Custom - but I can't seem to get it working!

public class ManufacturerMap : ClassMap<Manufacturer>
{
    public ManufacturerMap()
    {
        Id(x => x.ID).GeneratedBy.Custom(typeof(StringTableHiLoGenerator));
        Map(x => x.Name);
    }
}


public class StringTableHiLoGenerator : TableHiLoGenerator
{
    public override object Generate(ISessionImplementor session, object obj)
    {
        return base.Generate(session, obj).ToString();
    }
}

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

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

发布评论

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

评论(3

呆头 2024-08-16 21:23:05

我认为您无法让标准的 HiLo 生成器与字符串一起使用。看一下创建一个自定义 id 生成器(可以是带有字符串的 hilo):

http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate.aspx

有关您更新的更新

我在流利的维基中找不到任何与此相关的正确文档。不过,您可以尝试这个通用方法,而不是您正在使用的方法:

Id(x => x.Id).GeneratedBy.Custom<IdentityGenerator>()

这有效吗?如果没有,我认为如果您在 Fluent-nhibernate 邮件列表上发帖,您可能会得到最快的回复:

http: //groups.google.com/group/ Fluent-nhibernate

I don't think you will manage to get the standard HiLo generator working with a string. Take a look at creating a custom id generator (which could be a hilo with a string):

http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate.aspx

UPDATE regarding you update

I can't find any proper documentation regarding this in the fluent wiki. You could try this generic method though, rather than the method you are using:

Id(x => x.Id).GeneratedBy.Custom<IdentityGenerator>()

Does that work? If not I think your quickest response might come if you post on the fluent-nhibernate mailing list:

http://groups.google.com/group/fluent-nhibernate

我是男神闪亮亮 2024-08-16 21:23:05

发布的答案也适用于我的情况,我有一个字符串作为带有自定义 ID 生成器的主键,并且 Nhiberate 抛出错误:
“类型不是 ValueTypeType
参数名称:类型”。

The posted answer also worked in my case where i had a string as Primary Key with Custom Id Generator and Nhiberate was throwing the error:
"type is not a ValueTypeType
Parameter name: type".

喵星人汪星人 2024-08-16 21:23:04

终于破解了...感谢您的帮助 - 如果有人感兴趣,这里是解决方案...

注意:Configure 方法中 IType 必须作为 int 传递给基类。

public class ManufacturerMap : ClassMap<Manufacturer>
{
    public ManufacturerMap()
    {
        Id(x => x.ID).GeneratedBy.Custom<StringTableHiLoGenerator>(a => a.AddParam("max_lo", Nexus3General.HiLoGeneratorMaxLoSize.ToString()));
        Map(x => x.Name);
    }
}

public class StringTableHiLoGenerator : TableHiLoGenerator
{
    public override object Generate(ISessionImplementor session, object obj)
    {
        return base.Generate(session, obj).ToString();
    }

    public override void Configure(IType type, System.Collections.Generic.IDictionary<string, string> parms, NHibernate.Dialect.Dialect dialect)
    {
        base.Configure(NHibernateUtil.Int32, parms, dialect);
    }
}

Finally cracked it ... thanks for your assistance - here's the solution in case anyone's interested...

Note: that in the Configure method the IType has to be passed to the base as an int.

public class ManufacturerMap : ClassMap<Manufacturer>
{
    public ManufacturerMap()
    {
        Id(x => x.ID).GeneratedBy.Custom<StringTableHiLoGenerator>(a => a.AddParam("max_lo", Nexus3General.HiLoGeneratorMaxLoSize.ToString()));
        Map(x => x.Name);
    }
}

public class StringTableHiLoGenerator : TableHiLoGenerator
{
    public override object Generate(ISessionImplementor session, object obj)
    {
        return base.Generate(session, obj).ToString();
    }

    public override void Configure(IType type, System.Collections.Generic.IDictionary<string, string> parms, NHibernate.Dialect.Dialect dialect)
    {
        base.Configure(NHibernateUtil.Int32, parms, dialect);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文