2

I am getting following exception while accessing data form database using fluent NHibernate

An exception of type 'NHibernate.HibernateException' occurred in NHibernate.dll but was not handled in user code

Additional information: Can't Parse 1 as State

I have Enum type in my employee entity.

Employee

public class Employee
{
    public virtual Guid Id { get; set; }
    public virtual string Name { get; set; }
    public virtual DateTime DateOfBirth { get; set; }
    public virtual string ContactNumber  { get; set; }
    //[JsonConverter(typeof(StringEnumConverter))]
    public virtual State Status { set; get; }
    public virtual DateTime LastModify  { get; set; }
}

EmployeeMapping

public class EmployeeMap : ClassMapping<Employee>
{
    public EmployeeMap()

    {
        Table("Employee");

        Id(i => i.Id, m => m.Generator(Generators.GuidComb));

        Property(p => p.Name, m =>
        {
            m.NotNullable(false);
            m.Length(120);
        });

        Property(p => p.DateOfBirth, m => m.NotNullable(true));

        Property(p => p.ContactNumber, m =>
        {
            m.NotNullable(false);
            m.Length(12);
        });

        Property(p => p.Status, m => m.Column(y =>
        {
            y.NotNullable(true);
            y.Default(0);
        }));

        Version(v => v.LastModify, m =>
        {
            m.Column(y =>
            {
                y.NotNullable(true);
                y.Default("CURRENT_TIMESTAMP");
            });
            m.Type(NHibernateUtil.Timestamp);
            m.Generated(VersionGeneration.Never);
        });
    }       
}

State Enum

public enum State
{
    Probationer ,
    Permanent,
    Contractor
}

Please suggest me how to remove the exception.

Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
MANISH KUMAR CHOUDHARY
  • 3,396
  • 3
  • 22
  • 34

2 Answers2

2

Try this:

Property(x => x.Status, x => x.Type(typeof(State), null));
Alexey Zimarev
  • 17,944
  • 2
  • 55
  • 83
2

When I tried using NHibernate Mapping by Code using the approach from Alexey or this:

Property(x => x.Command, o =>
{
  o.Type<CommandType>();
});

I got a mapping error along the lines of 'must implement IUserType. The correct way to map an enum is:

Property(x => x.Command, o =>
{
  o.Type<EnumType<CommandType>>();
});
GrahamB
  • 1,368
  • 15
  • 35