1

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?

Prabu
  • 4,097
  • 5
  • 45
  • 66

3 Answers3

4

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);
       });
Jason Morse
  • 6,204
  • 5
  • 29
  • 29
1

Use NHibernate.Type.TypeFactory.GetAnsiStringType(someLength)

Darren Kopp
  • 76,581
  • 9
  • 79
  • 93
  • It's not working for me. map.Type is expecting an IIdentifierType, while the TypeFactory is giving a NullableType. – Prabu Nov 05 '12 at 21:00
  • Use @Jason Morse's answer. That method returns NullableType, though it's returning AnsiStringType which implements IIdentifierType. – Darren Kopp Nov 06 '12 at 18:56
0

NHibernate.Type.TypeFactory.GetAnsiStringType(someLength) did not work in case of SetParameterList() so I changed it to NHibernateUtil.AnsiString. This changed the parameter to varchar(8000) which is still ok instead of nvarchar(4000) which was causing issues with change in execution plan choice.

Sharath
  • 516
  • 1
  • 8
  • 19