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.

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();
    }
}



Finally cracked it ... thanks for you assistance - heres the solution in case anyones 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);
    }
}
 
 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值