![](/img/trans.png)
[英]ASP.net AutoMapper Missing type map configuration or unsupported mapping
[英].Net Core Automapper missing type map configuration or unsupported mapping
网络核心应用。 我正在尝试使用 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:缺少类型 map 配置或不支持的映射。
有人可以帮我理解这个错误吗? 任何帮助将不胜感激。 谢谢
this.CreateMap<Model1, Model2>();
将创建 map 从Model1
到Model2
,所以这应该工作:
Model2 model = this.Mapper.Map<Model2>(new Model1());
如果您想要反之亦然,请将注册更改为:
this.CreateMap<Model2, Model1>();
或添加ReverseMap
以具有双向之一:
this.CreateMap<Model1, Model2>().ReverseMap();
我有同样的错误。 我花了几个小时,所以我想我会在这里提到它。
对于 Api、域/实体、DTO 和映射配置文件,我有不同的项目。
有两个问题。 首先,我忘记将 DTO 映射配置文件类标记为公共的。 其次,我在启动 class 中注册了自动映射器,如下所示:
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
如果您的映射配置文件在同一个程序集中,上述行工作正常。 在我的情况下,映射配置文件位于不同的程序集中,因此这不是拾取配置文件。 为了解决这个问题,我使用了以下内容:
services.AddAutoMapper(Assembly.Load("Your assembly name"));
在一些教程中,而不是
var mappingConfig = new MapperConfiguration(mc =>
{
mc.AddProfile(new MappingProfile());
});
IMapper mapper = mappingConfig.CreateMapper();
services.AddSingleton(mapper);
使用
service.AddAutoMapper({profile assembly marker types});
通常,在那些教程中,他们指的是启动。 实际上,装配标记应该是您的配置文件映射 class。 在这种情况下,应该是
service.AddAutoMapper(typeof(MappingProfile));
使用这条线
var getusers = users.Select(c => _mapper.Map<GetUsersDto>(c)).ToList();
而不是这个
var getusers = _mapper.Map<GetUsersDto>(users);
在 net core 6 中,我用上面的 @Amit 推荐解决了这个问题。
它是,在 Proram.cs 我有: builder.Services.AddAutoMapper(typeof(Program));
正如 automapper 页面推荐的那样,我收到了缺少的映射异常。
我更改为以下并解决了问题:
builder.Services.AddAutoMapper(typeof(AutoMapperProfile));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.