简体   繁体   English

自动映射器从 DateTimeOffSet 到 DateTime 并反向

[英]Automapper from DateTimeOffSet to DateTime and reverse

i have a publicDto class like this:我有一个 publicDto class 像这样:

    [System.CodeDom.Compiler.GeneratedCode("NJsonSchema", "10.0.22.0 (Newtonsoft.Json v11.0.0.0)")]
    public partial class SubjectAddressDataDto 
    {
        
        [Newtonsoft.Json.JsonProperty("country", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        [Newtonsoft.Json.JsonConverter(typeof(Newtonsoft.Json.Converters.StringEnumConverter))]
        public CountryCodeDto Country { get; set; }
    
        /// <summary>The addrees data are valid since this date</summary>
        [Newtonsoft.Json.JsonProperty("sinceDate", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
        [Newtonsoft.Json.JsonConverter(typeof(DateFormatConverter))]
        public System.DateTimeOffset SinceDate { get; set; }
    
        private System.Collections.Generic.IDictionary<string, object> _additionalProperties = new System.Collections.Generic.Dictionary<string, object>();
    
        [Newtonsoft.Json.JsonExtensionData]
        public System.Collections.Generic.IDictionary<string, object> AdditionalProperties
        {
            get { return _additionalProperties; }
            set { _additionalProperties = value; }
        }
    
    
    }

and and internal model class to automapper like this:和内部 model class 到自动映射器,如下所示:

    public class SubjectAddressDataDto
    {
        public string FullAddress { get; set; }
        public string Street { get; set; }
        public string Locality { get; set; }
        public string Region { get; set; }
      
        public string PostalCode { get; set; }
        public string Country { get; set; }

        public DateTime SinceDate { get; set; }
    }
}

in automapping i receive the following error:在自动映射中,我收到以下错误:

 ---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
  
  Mapping types:
  DateTimeOffset -> DateTime
  System.DateTimeOffset -> System.DateTime
  
  Destination Member:
  SinceDate 

anyone could help me to define a correct mapping config to solve it?谁能帮我定义一个正确的映射配置来解决它?

Thank a lot.非常感谢。

you can create a custom type converter with implement of ITypeConverter<TSource, TDestination> interface and add it to your profile class with ConvertUsing method.您可以使用实现 ITypeConverter<TSource, Tdestination> 接口创建自定义类型转换器,并使用 ConvertUsing 方法将其添加到您的配置文件 class 中。 Doc from automapper: custom type converters来自 automapper 的文档: 自定义类型转换器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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