- Posted by liammclennan on May 15, 2009
In general, I like to use enums for model properties that have a fixed set of values, and I like to persist those enums are strings. The advantage is that the values can be read without requiring a pointless mapping.
Because I am using Fluent NHibernate Automapping it was not straightforward to get this to work. The trick is to specifically instruct the automapping to map the enum property. Without this, the property is simply ignored. Here is my automapping configuration, the enum is the User.Role property (line 7):
private static AutoPersistenceModel Automapping()
{
return AutoPersistenceModel.MapEntitiesFromAssemblyOf<Entity>()
.ConventionDiscovery.Add<OneToManyConvention>()
.ConventionDiscovery.Add<NotNullPropertyConvention>()
.WithSetup(s => s.IsBaseType = type => type == typeof(Entity) )
.ForTypesThatDeriveFrom<User>(map => map.Map(u => u.Role))
.Where(t => t.Namespace == "MyNamespace.Domain");
}
The other interesting mapping here is line 6. It instructs the automapper that every domain class inherits from Entity.