简体   繁体   English

使NHibernate生成HiLo字符串ID

[英]Getting NHibernate to generate a HiLo string ID

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. 我们有一个大型系统,它通过Unity松散地绑定到其数据源(Navision)-我们有机会将其换出并拥有我们自己的数据库。

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. 因此,我们进行了四处浏览,非常喜欢Fluent NHibernate的外观-我们正在尝试获得概念证明,并交换了一些服务。

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. 我们想使用NHibernates HiLo算法-不幸的是,我们从Navision继承了字符串ID,该字符串以ID为前缀(例如COL00001),因此要匹配接口,我们需要使用字符串ID。

Does anyone know how I'd get something like ... 有谁知道我会得到像...

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

working where ID is a string? 工作在哪里ID是一个字符串? We're currently getting Identity must be int, long etc 我们目前正在获得Identity必须为int,long等

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! 我尝试了建议的文章中的示例,但此功能已从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();
    }
}

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 . 注意: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);
    }
}

I don't think you will manage to get the standard HiLo generator working with a string. 我认为您不会设法使标准HiLo生成器与字符串一起使用。 Take a look at creating a custom id generator (which could be a hilo with a string): 看一下创建自定义id生成器(可以是带有字符串的hilo):

http://nhforge.org/wikis/howtonh/creating-a-custom-id-generator-for-nhibernate.aspx 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. 在流畅的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 http://groups.google.com/group/fluent-nhibernate

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM