【问题标题】:Automapper unsupported mapping int16 to enumAutomapper 不支持将 int16 映射到枚举
【发布时间】:2015-11-26 04:05:58
【问题描述】:
  • AutoMapper 4.1.1

源对象:

public class Platform_ContentTemplatesModel : OzEfEntity, IEntity<int>
{
    public string TemplateContent { get; set; } 
    public int TemplateIdentifier { get; set; } 
    public short WebsitePropertyId { get; set; }
    public int Id { get; set; } 
}

目标对象:

public class OzCpPlatformContentTemplateItemRecord
{
    public int Id { get; set; }
    public string TemplateContent { get; set; }
    public ContentTemplateIdentifierEnum TemplateIdentifier { get; set; }
    public WebsitePropertyEnum WebsiteProperty { get; set; }
}

映射配置:

Mapper.CreateMap<Platform_ContentTemplatesModel, OzCpPlatformContentTemplateItemRecord>()
                .ForMember(dest => dest.WebsiteProperty, opt => opt.MapFrom(src => src.WebsitePropertyId));

现在 TemplateIdentifier 从 intenum 的映射完美运行。然而,WebsitePropertyid 到 WebsiteProperty 的映射,即 shortenum 的映射失败,但有以下例外:

{"缺少类型映射配置或不支持 映射。 映射类型: Int16 -> 网站属性枚举 System.Int16 -> 网站属性枚举 目的地路径: OzCpPlatformContentTemplateItemRecord.WebsiteProperty.WebsiteProperty 源值:1"}

现在我有一个值为 1 的枚举成员。这里的问题是基础类型是 short。我无法将其更改为 int,那么我该如何解决这个问题?

【问题讨论】:

    标签: c# automapper


    【解决方案1】:

    确保您的目标枚举映射到一个简短的

    public enum WebsitePropertyEnum : short
    {
        thing1 = 0,
        thing2 = 1
    }
    

    【讨论】:

    • 传奇!!这就是诀窍。关于枚举可以从另一种类型下降这一事实的任何文档?
    • 检查 microsoft 枚举 C# 页面 msdn.microsoft.com/en-us/library/sbbt4032.aspx "已批准的枚举类型为 byte、sbyte、short、ushort、int、uint、long 或 ulong。"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 2021-09-17
    • 2013-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    相关资源
    最近更新 更多