Automapper 不支持将 int16 映射到枚举
Automapper unsupported mapping int16 to enum
- 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 从 int 到 enum 的映射完美无缺。然而,WebsitePropertyid 到 WebsiteProperty 的映射,即 short 到 enum 的映射失败,出现以下异常:
{"Missing type map configuration or unsupported
mapping.
Mapping types:
Int16 -> WebsitePropertyEnum
System.Int16 -> WebsitePropertyEnum
Destination path:
OzCpPlatformContentTemplateItemRecord.WebsiteProperty.WebsiteProperty
Source value:1"}
现在我有一个值为 1 的枚举成员。所以这里的问题是基础类型是 short。我无法将其更改为 int 那么我该如何解决这个问题?
确保您的目标枚举映射到短
public enum WebsitePropertyEnum : short
{
thing1 = 0,
thing2 = 1
}
- 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 从 int 到 enum 的映射完美无缺。然而,WebsitePropertyid 到 WebsiteProperty 的映射,即 short 到 enum 的映射失败,出现以下异常:
{"Missing type map configuration or unsupported mapping. Mapping types: Int16 -> WebsitePropertyEnum System.Int16 -> WebsitePropertyEnum Destination path: OzCpPlatformContentTemplateItemRecord.WebsiteProperty.WebsiteProperty Source value:1"}
现在我有一个值为 1 的枚举成员。所以这里的问题是基础类型是 short。我无法将其更改为 int 那么我该如何解决这个问题?
确保您的目标枚举映射到短
public enum WebsitePropertyEnum : short
{
thing1 = 0,
thing2 = 1
}