Monday, November 11, 2013

Why Fluent NHibernate tries to write enums as strings?

This is frequently asked question in Stack Overflow site. The answer is very simple: you should specify the enum type in your mappings using CustomType<T>() for example CustomType<MyEnumType>(). This will write integers into the database then you save mapped object. Here follows example mappings:

public class UserMap : ClassMap<User>
    public UserMap()
        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.LoginName).Length(64).Nullable();
        Map(x => x.EMail).Length(128).Nullable();
        Map(x => x.FirstName).Length(128).Nullable();
        Map(x => x.LastName).Length(128).Nullable();
        Map(x => x.Birthday).Nullable();
        Map(x => x.Role).CustomType<Roles>().Not.Nullable();
        References(x => x.Employer).Column("EmployerId").LazyLoad().Nullable();