Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping DayOfWeek from the System namespace using Fluent Nhibernate

I'm currently working on an MVC 3 project which uses Fluent NHibernate. I utilise the System.DayOfWeek enum but when mapping this I received the following error -

Stack Trace: 

[MappingException: Could not determine type for: DayOfWeek, for columns:      NHibernate.Mapping.Column(WeekStart)]
NHibernate.Mapping.SimpleValue.get_Type() +456
NHibernate.Mapping.SimpleValue.IsValid(IMapping mapping) +40
NHibernate.Mapping.PersistentClass.Validate(IMapping mapping) +123
NHibernate.Mapping.RootClass.Validate(IMapping mapping) +24
NHibernate.Cfg.Configuration.ValidateEntities() +280
NHibernate.Cfg.Configuration.BuildSessionFactory() +43
FluentNHibernate.Cfg.FluentConfiguration.BuildSessionFactory() +54

[FluentConfigurationException: An invalid or incomplete configuration was used while      creating a SessionFactory. 
Check PotentialReasons collection, and InnerException for more detail.

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET     
Version:4.0.30319.272 

Example usage -

Map(x => x.WeekStart).CustomType(typeof(DayOfWeek));

I've seen this specific question asked on mailing lists and on stackoverflow but the nuances of it never seemed to be fully grasped and the person asking the question is referred to how to use a custom type mapping.

I am well aware of how to use the customtype functionality on fluent maps and frequently make use of it. However I don't understand why this enum in particular cannot be mapped. I presume it has something to do with the System namespace to which it belongs?

If anyone can shed any light on this I'd be most happy.

Thanks

like image 379
rsw Avatar asked Mar 13 '12 15:03

rsw


2 Answers

I got the mapping of DayOfWeek enum to work with this mapping.

mapping.Map(x => x.DayOfWeek);

and its maps fine to a nvarchar(255) in my mssql database.

My property looklikes this

public virtual DayOfWeek DayOfWeek { get; set; }

I use NHibernate version: 3.3.1.4000 and Fluent Nhibernate version: 1.3.0.733

like image 106
fito Avatar answered Nov 06 '22 07:11

fito


As with any enum mapping with NHibernate, save yourself some grief (dirty reads which cause unnecessary writes, different type specified in database, etc.) and just always use a PersistentEnumType.

In this example, create your PersistentEnumType:

public class NHibernateDayOfWeekEnumMapper : global::NHibernate.Type.PersistentEnumType
{
    public NHibernateDayOfWeekEnumMapper()
        : base(typeof(DayOfWeek))
    {

    }
}

Then use it to do your mapping:

Map(x => x.DayOfWeek).CustomType<NHibernateDayOfWeekEnumMapper>().Not.Nullable();

Now you've got the entity that doesn't dirty read (and so no unnecessary writes) and a correct type mapping in the database (when you use SchemaExport to generate your schema).

For more information see an oldie but goody at http://eashi.wordpress.com/2008/08/19/mapping-enumeration-of-type-int-in-nhibernate/

like image 33
Ted Avatar answered Nov 06 '22 07:11

Ted