Csharp/C#教程:entity framework6代码优先 – 自定义类型映射分享


entity framework6代码优先 – 自定义类型映射

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

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

伪:

 public class PhysicalAddressMap : ComplexType() { 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) 

使用处理转换的映射字符串属性包装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; } 

更新:用于评论关于在类中包装function的注释的示例代码

上述就是C#学习教程:entity framework6代码优先 – 自定义类型映射分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

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

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1043035.html

(0)
上一篇 2022年1月31日
下一篇 2022年1月31日

精彩推荐