c# - .Net Core Automapper 缺少类型映射配置或不支持的映射
问题描述
网络核心应用。我正在尝试使用 Auto mapper,但导致以下错误。
.Net Core Automapper missing type map configuration or unsupported mapping
我在 startup.cs 中有以下设置
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
然后我正在使用配置文件。
public class MappingProfile : Profile
{
public MappingProfile()
{
this.CreateMap<Geography, GeographyEntity>();
this.CreateMap<Model1, Model2>();
}
}
我正在使用自动映射器,如下所示
Model1 model = this.Mapper.Map<Model1>(Model2);
下面是型号
public partial class Model1
{
public int SNo { get; set; }
public string SarNo { get; set; }
public string SiteName { get; set; }
public string Client { get; set; }
public int CId { get; set; }
public DateTime StartDate { get; set; }
public bool IsActive { get; set; }
public virtual Model2 C { get; set; }
}
public class Model2
{
public int SNo { get; set; }
public string SarNo { get; set; }
public string SiteName { get; set; }
public int CId { get; set; }
public string Client { get; set; }
public bool? IsActive { get; set; }
public DateTime StartDate { get; set; }
}
我在自动映射器中遇到以下错误。
AutoMapper.AutoMapperMappingException:缺少类型映射配置或不支持的映射。
有人可以帮我理解这个错误吗?任何帮助将不胜感激。谢谢
解决方案
this.CreateMap<Model1, Model2>();
将从Model1
to创建地图Model2
,所以这应该工作:
Model2 model = this.Mapper.Map<Model2>(new Model1());
如果您想要反之亦然,请将注册更改为:
this.CreateMap<Model2, Model1>();
或添加ReverseMap
双向:
this.CreateMap<Model1, Model2>().ReverseMap();
推荐阅读
- zsh - zsh _arguments:带有不同数量参数的 --option 和之后的 --other-option
- bash - 如何使用带有 bash 脚本和 xdotool 的 Thunderbird 发送自动邮件?
- mule - HTTPS 连接器上无法识别 Spring 属性
- python - AttributeError: 'QComboBox' 对象没有属性 'setPlaceholderText' 我该如何解决这个问题?
- django - Django - 编辑或删除表中的选定行
- c++ - 使用 void 函数时没有输出
- python-3.x - matplotlib python datetime TypeError
- android - 在 Flutter 上的同一个项目上创建它的 android 版本的 apk 后,我可以构建一个 iOS 应用程序吗?
- c++ - 从十六进制字符串形成字符串
- cypher - 带有子路径的密码查询并存在于redisgraph中