c# - Entity Framework 6 Code First - 自定义类型映射

标签 c# database entity-framework

我在我的域中使用了一些模型,这些模型对序列化或映射不是很友好,例如 System.Net.* 命名空间中的结构或类。

现在我想知道是否可以在 Entity Framework 中定义自定义类型映射。

伪:

public class PhysicalAddressMap : ComplexType<PhysicalAddress>() {
    public PhysicalAddressMap() {

        this.Map(x => new { x.ToString(":") });
        this.From(x => PhysicalAddress.Parse(x));
    }
}

期望的结果:

SomeEntityId       SomeProp         PhysicalAddress    SomeProp
------------------------------------------------------------------
           4          blubb       00:00:00:C0:FF:EE        blah

                                           ^
                                           |
                             // PhysicalAddress got mapped as "string"
                             // and will be retrieved by
                             // PhysicalAddress.Parse(string value)

最佳答案

用处理转换的映射字符串属性包装类型为 PhysicalAddressNotMapped 属性:

    [Column("PhysicalAddress")]
    [MaxLength(17)]
    public string PhysicalAddressString
    {
        get
        {
            return PhysicalAddress.ToString();
        }
        set
        {
            PhysicalAddress = System.Net.NetworkInformation.PhysicalAddress.Parse( value );
        }
    }

    [NotMapped]
    public System.Net.NetworkInformation.PhysicalAddress PhysicalAddress
    {
        get;
        set;
    }

更新:用于询问类中包装功能的注释示例代码

[ComplexType]
public class WrappedPhysicalAddress
{
    [MaxLength( 17 )]
    public string PhysicalAddressString
    {
        get
        {
            return PhysicalAddress == null ? null : PhysicalAddress.ToString();
        }
        set
        {
            PhysicalAddress = value == null ? null : System.Net.NetworkInformation.PhysicalAddress.Parse( value );
        }
    }

    [NotMapped]
    public System.Net.NetworkInformation.PhysicalAddress PhysicalAddress
    {
        get;
        set;
    }

    public static implicit operator string( WrappedPhysicalAddress target )
    {
        return target.ToString();
    }

    public static implicit operator System.Net.NetworkInformation.PhysicalAddress( WrappedPhysicalAddress target )
    {
        return target.PhysicalAddress;
    }

    public static implicit operator WrappedPhysicalAddress( string target )
    {
        return new WrappedPhysicalAddress() 
        { 
            PhysicalAddressString = target 
        };
    }

    public static implicit operator WrappedPhysicalAddress( System.Net.NetworkInformation.PhysicalAddress target )
    {
        return new WrappedPhysicalAddress()
        {
            PhysicalAddress = target
        };
    }

    public override string ToString()
    {
        return PhysicalAddressString;
    }
}

关于c# - Entity Framework 6 Code First - 自定义类型映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22622502/

相关文章:

nhibernate - Entity Framework 和 NHibernate - 缓存仍然是服务层的职责吗?

entity-framework - DbMigrator 尝试执行每个迁移,而不是检测已执行的迁移

c# - ASP.Net Video 中的 DataList 应在点击时放大

c# - 从我的代码中“抛出警告”

c# - 使用 SQL 数据库及其表填充 Treeview

mysql - 高效查询一对多关系

c# - 只有一个消费者从 RabbitMQ 上的队列接收消息

c# - 如何从JSON格式的DataSet中提取DataTable

ios - 架构 i386 : "_sqlite3_open", 的 undefined symbol 引用自:错误

silverlight - 使用RIA Services域服务的EF4中的Include()无法加载!