0

I have a class

public class Site {
  public DateTime SiteMonth {get; set;}
}

in the database the SiteMonth is represented as an integer in yyyymm format.

Is there a way to map this in NHibernate without introducing a new property on my Site class?

2 Answers 2

2

Yes - make a simple IUserType that maps between your integer format and a DateTime. Then set the type attribute on the property element to the AssemblyQualifiedName of that user type.

Ayende has an example on how to implement a user type.

Oh yeah, and if you are using Fluent NHibernate to do your mappings, you can do it like this:

Map(d => d.MyFunkyWeirdLegacyDateTime)
    .SetAttribute("type", typeof(MyCustomDateTime).AssemblyQualifiedName);
Sign up to request clarification or add additional context in comments.

2 Comments

CustomTypeIs<MyCustomDateTime>() is the preferred method, instead of SetAttribute.
Ah, thanks for pointing that out! Fluent NHibernate is smashing, by the way :-)
1

I was just in the Fluent NHibernate Wiki and came across this, which is in the AutoMapping conventions, but in the Fluent Mapping Conventions section it mentioned that the AutoMapping Conventions will work for Fluent Mapping as well.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.