实体框架6代码第一 - 自定义类型映射 [英] Entity Framework 6 Code First - Custom Type Mapping

查看:129
本文介绍了实体框架6代码第一 - 自定义类型映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



Pseudo:

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

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

希望的结果: / p>

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

^
|
// PhysicalAddress被映射为string
//并且将被
检索// PhysicalAddress.Parse(string value)
pre>

解决方案

包装 NotMapped 属性> 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;
}
}


I'm using some models inside my domain which are not very serialization- or mapping-friendly, such as structs or classes from the System.Net.* namespace.

Now I'm wondering if it's possible to define custom type mappings in Entity Framework.

Pseudo:

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

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

Desired result:

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)

解决方案

wrap a NotMapped property of type PhysicalAddress with a mapped string property that handles the conversions:

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

Update: example code for comment asking about wrapping functionality in a class

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

这篇关于实体框架6代码第一 - 自定义类型映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆