Search code examples
c#nhibernateormnhibernate-mapping-by-code

NHibernate 3.3: Setting the type to Ansistring in an Id for Mapping by code


I am mapping the following entity as such:

public class PersonEntity
{
    public virtual string PersonId { get; set; }

    public virtual String Salutation { get; set; }
    public virtual String FirstName { get; set; }
    public virtual String LastName { get; set; }
    public virtual DateTime Birthdate { get; set; }
}

public class PersonMap : ClassMapping<PersonEntity>
{
    public PersonMap()
    {
        //ComponentAsId(i => i.Key, map => map.Property(p => p.PersonId, m => m.Type(NHibernateUtil.AnsiString)));

        Id(i => i.PersonId, map => map.Type(???)));
        Property(i => i.Salutation);
        Property(i => i.FirstName);
        Property(i => i.LastName);
        Property(i => i.Birthdate);
    }
}

As you can see in the commented out code, I can use the NHibernateUtil to set the type as AnsiString when using components as an Id. However I cannot figure out what to do in plain Id mappings.

I have tried using new NHibernate.Type.AnsiStringType(), but this complains about not having no constructors being defined for it.

Any ideas guys?


Solution

  • I had to explicitly cast the AnsiStringType to IIdentifierType and also set the length property.

           Id(x => x.Id,
           m =>
           {
               m.Generator(Generators.Assigned);
               m.Type((IIdentifierType)TypeFactory.GetAnsiStringType(16));
               m.Length(16);
           });