【问题标题】:Entity Framework 6 Code First - Custom Type MappingEntity Framework 6 Code First - 自定义类型映射
【发布时间】:2015-11-03 04:20:32
【问题描述】:

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

现在我想知道是否可以在实体框架中定义自定义类型映射。

伪:

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)

【问题讨论】:

    标签: c# database entity-framework


    【解决方案1】:

    使用处理转换的映射字符串属性包装PhysicalAddress 类型的NotMapped 属性:

        [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;
        }
    }
    

    【讨论】:

    • 有没有办法将这种行为包装在一个类中以重用它?
    • 当然 - 创建一个具有相关转换运算符的复杂类型。在解决方案中添加了示例代码。
    • 好的,谢谢。我现在遇到的问题是,由于向后兼容性,我无法重命名数据库列,所以我想将每个 WrappedPhysicalAddress 类型的属性映射到自定义列
    • 有属性? (我不喜欢流利的api)
    • 几乎不支持任何 EF 表达式树。
    猜你喜欢
    • 2013-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    相关资源
    最近更新 更多