EF4 equivalent of NHibernate IUserType

355 views Asked by At

I want to map a model property of type TimeZoneInfo to a column in the database. In NHib, I just made an IUserType "TimeZoneInfoString" that converted back and forth and then used a typedef. How can I do this type of work using Entity Framework 4.0?

1

There are 1 answers

7
Ladislav Mrnka On BEST ANSWER

Entity framework doesn't have equivalent to NHibernate's user types. You must create separate property in your entity for it and map only the string property. Somethink like:

public partial class MyEntity
{
    public TimeZoneInfo TimeZone
    {
        get
        {
            return Parse(TimeZoneInfoString);
        }
        set
        {
            TimeZoneInfoString = value.ToString();
        }
    }
}

Where this class is your partial part to autogenerated entity. TimeZoneInfoString is property mapped in your entity and Parse and ToString contains your conversion logic.