0
votes

Met a problem with NHibernate and enums. I have a simple entity with enumType property:

public virtual SchemaStatus Status
    {
        get;
        set;
    }
public enum SchemaStatus
{
    PREP,
    BGN,
    FAIL,
    CREA
}

And there is a mapping:

 Map(x => x.Status)
     .Column("Status")
     .Nullable();

And what happens when status column in db table is null? NHibernate returns first value from enum, its PREP here. So my question is how to prevent nhibernate from returning first enum value instead of null when property is of enum type?

1

1 Answers

2
votes

The code you show, only states that NULL values are allowed in your database. When such a NULL is encountered, what enum value should it then map to? If you want it to be NULL, you should change Status into a nullable SchemaStatus.

Try something like:

public virtual SchemaStatus? Status
{
    get;
    set;
}

I am not at a PC right now, so I cannot try it - but it should be something like it.