简体   繁体   English

相当于EFiber的NHibernate IUserType

[英]EF4 equivalent of NHibernate IUserType

I want to map a model property of type TimeZoneInfo to a column in the database. 我想将TimeZoneInfo类型的模型属性映射到数据库中的列。 In NHib, I just made an IUserType "TimeZoneInfoString" that converted back and forth and then used a typedef. 在NHib中,我只是制作了一个IUserType“ TimeZoneInfoString”,它来回转换,然后使用typedef。 How can I do this type of work using Entity Framework 4.0? 如何使用Entity Framework 4.0进行此类工作?

Entity framework doesn't have equivalent to NHibernate's user types. 实体框架不具有NHibernate的用户类型。 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. TimeZoneInfoString是在您的实体中映射的属性,并且ParseToString包含您的转换逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM