Getting NHibernate to generate a HiLo string ID

Source: Internet
Author: User

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

So we ' ve had a look around and really like the look of the Fluent Nhibernate-we ' re trying to get a proof of concept going an D 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-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 and this functionality have been removed from later versions of Fluent Nhibern Ate-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 do assistance-heres the solution in case anyones interested?

Note that the Configure method the IType have to is passed to the base as a 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);    }}

Getting NHibernate to generate a HiLo string ID

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.