实体框架6代码优先——自定义类型映射结构或类。现在我想知道是否可以在EntityFramework中定义自定义类型映射。伪:publicclassPhysicalAddressMap:ComplexType(){publicPhysicalAddressMap(){this.Map(x=>new{x.ToString(":")});this.From(x=>PhysicalAddress.Parse(x));预期结果:SomeEntityIdSomePropPhysicalAddressSomeProp---------------------------------------------------------------4blubb00:00:00:C0:FF:EE等等^|//PhysicalAddress被映射为“string”//并将被检索//PhysicalAddress.Parse(stringvalue)使用处理转换的映射字符串属性包装PhysicalAddress类型的NotMapped属性:[Column("PhysicalAddress")][MaxLength(17)]publicstringPhysicalAddressString{get{returnPhysicalAddress.ToString();}set{PhysicalAddress=System.Net.NetworkInformation.PhysicalAddress.Parse(值);}}[NotMapped]publicSystem.Net.NetworkInformation.PhysicalAddressPhysicalAddress{get;放;}更新:与以上是C#学习教程:Entityframework6代码先行-自定义类型映射分享的全部内容。如果对大家有用,需要了解更多C#学习教程,希望大家多多关注——【ComplexType】publicclassWrappedPhysicalAddress{[MaxLength(17)]publicstringPhysicalAddressString{get{returnPhysicalAddress==null?null:PhysicalAddress.ToString();}set{PhysicalAddress=value==null?null:System.Net.NetworkInformation.PhysicalAddress.Parse(值);}}[NotMapped]publicSystem.Net.NetworkInformation.PhysicalAddressPhysicalAddress{get;放;}publicstaticimplicitoperatorstring(WrappedPhysicalAddresstarget){returntarget.ToString();}publicstaticimplicitoperatorSystem.Net.NetworkInformation.PhysicalAddress(WrappedPhysicalAddresstarget){returntarget.PhysicalAddress;}publicstaticimplicitoperatorWrappedPhysicalAddress(stringtarget){returnnewWrappedPhysicalAddress(){PhysicalAddressString=target};}publicstaticimplicitoperatorWrappedPhysicalAddress(System.Net.NetworkInformation.PhysicalAddresstarget){returnnewWrappedPhysicalAddress(){PhysicalAddress=target};}公共overridestringToString(){returnPhysicalAddressString;}}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
