简体   繁体   中英

Entity Framework 6 Code First - Custom Type Mapping

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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