简体   繁体   English

Automapper不支持将int16映射到枚举

[英]Automapper unsupported mapping int16 to enum

  • AutoMapper 4.1.1 AutoMapper 4.1.1

Source Object: 源对象:

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

Destination object: 目标对象:

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

Mapping configuraton: 映射配置:

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

Now the mapping of TemplateIdentifier from an int to the enum works perfectly. 现在,TemplateIdentifier从int枚举的映射可以正常工作。 However the mapping of WebsitePropertyid to WebsiteProperty, namely a short to an enum fails with the following exception: 但是,WebsitePropertyid到WebsiteProperty的映射(即枚举缩写)失败,但以下情况除外:

{"Missing type map configuration or unsupported mapping. Mapping types: Int16 -> WebsitePropertyEnum System.Int16 -> WebsitePropertyEnum Destination path: OzCpPlatformContentTemplateItemRecord.WebsiteProperty.WebsiteProperty Source value:1"} {“缺少类型映射配置或不支持的映射。映射类型:Int16-> WebsitePropertyEnum System.Int16-> WebsitePropertyEnum目标路径:OzCpPlatformContentTemplateItemRecord.WebsiteProperty.WebsiteProperty源值:1”}

Now I have an enum member with the value of 1. So is the issue here that the underlying type is a short . 现在,我有一个枚举成员,其值为1。所以这里的问题是基础类型是short I cannot change this to an int so how do I work around this? 我不能将其更改为int ,如何解决此问题?

Make sure that your destination enum maps to a short 确保您的目标枚举映射到一个简短的

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

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

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