.Net Core Automapper 缺少类型映射配置或不支持的映射
.Net Core Automapper missing type map configuration or unsupported mapping
网络核心应用。我正在尝试使用自动映射器,但导致以下错误。
.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: Missing type map configuration
or unsupported mapping.
有人可以帮我理解这个错误吗?任何帮助将不胜感激。谢谢
this.CreateMap<Model1, Model2>();
将创建从 Model1
到 Model2
的映射,因此这应该有效:
Model2 model = this.Mapper.Map<Model2>(new Model1());
如果您想要反之亦然,请将注册更改为:
this.CreateMap<Model2, Model1>();
或添加ReverseMap
以获得双向一个:
this.CreateMap<Model1, Model2>().ReverseMap();
在一些教程中而不是
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));
我有同样的错误。我花了几个小时所以我想我会在这里提到它。
我有 Api、domains/entities、DTO 和映射配置文件的不同项目。
有两个问题。
首先,我忘记将 DTO 映射配置文件 classes 标记为 public。
其次,我在启动 class 中注册了自动映射器,如下所示:
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
如果您的映射配置文件位于同一个程序集中,则以上行可以正常工作。在我的例子中,映射配置文件位于不同的程序集中,因此它没有获取配置文件。为了解决这个问题,我使用了以下内容:
services.AddAutoMapper(Assembly.Load("Your assembly name"));
网络核心应用。我正在尝试使用自动映射器,但导致以下错误。
.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: Missing type map configuration or unsupported mapping.
有人可以帮我理解这个错误吗?任何帮助将不胜感激。谢谢
this.CreateMap<Model1, Model2>();
将创建从 Model1
到 Model2
的映射,因此这应该有效:
Model2 model = this.Mapper.Map<Model2>(new Model1());
如果您想要反之亦然,请将注册更改为:
this.CreateMap<Model2, Model1>();
或添加ReverseMap
以获得双向一个:
this.CreateMap<Model1, Model2>().ReverseMap();
在一些教程中而不是
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));
我有同样的错误。我花了几个小时所以我想我会在这里提到它。
我有 Api、domains/entities、DTO 和映射配置文件的不同项目。
有两个问题。 首先,我忘记将 DTO 映射配置文件 classes 标记为 public。 其次,我在启动 class 中注册了自动映射器,如下所示:
services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
如果您的映射配置文件位于同一个程序集中,则以上行可以正常工作。在我的例子中,映射配置文件位于不同的程序集中,因此它没有获取配置文件。为了解决这个问题,我使用了以下内容:
services.AddAutoMapper(Assembly.Load("Your assembly name"));