无法将 Auto Mapper 与 .net core 2.2 一起使用
Unable to use Auto Mapper with .net core 2.2
我无法在 .net core 2.2 中使用 Auto Mapper。它一直抛出这个错误:
Unmapped members were found. Review the types and members below. Add a
custom mapping expression, ignore, add a custom resolver, or modify
the source/destination type For no matching constructor, add a no-arg
ctor, add optional arguments, or map all of the constructor parameters
======================================================================================================================================================================================================= AutoMapper created this type map for you, but your types cannot be
mapped using the current configuration. IDataReader -> List1
(Destination member list) System.Data.IDataReader ->
System.Collections.Generic.List
1[[Models.EngineModel,
Models, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]] (Destination member list)
Unmapped properties: Capacity
这是我的引擎模型 class:
public class EngineModel
{
public string Id { get; set; }
public int engineNo { get; set; }
public string engineHost { get; set; }
}
这是我的个人资料:
public class EngineMappingProfile : Profile
{
public EngineMappingProfile()
{
CreateMap<IDataReader, EngineModel>()
.ForMember(dest => dest.Id , opt => opt.MapFrom(src => src.GetString(src.GetOrdinal("ID"))))
.ForMember(dest => dest.engineNo , opt => opt.MapFrom(src => src.GetInt32(src.GetOrdinal("ENG_NO"))))
.ForMember(dest => dest.engineHost , opt => opt.MapFrom(src => src.GetString(src.GetOrdinal("ENG_HOST"))))
}
}
这是我的 Startup.cs ConfigureServices 方法:
public void ConfigureServices(IServiceCollection services)
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<EngineMappingProfile>();
});
services.AddAutoMapper();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(options =>
{
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});
}
以上所有代码都在我的 API 项目中。我在我的商业项目中使用这些配置文件:
var enginesData = AutoMapper.Mapper.Map<IDataReader, List<EngineModel>>(dataReader.mcDataReader);
我正在使用 "AutoMapper.Extensions.Microsoft.DependencyInjection (6.0.0)" 和“AutoMapper (8.0.0)
AutoMapper 正在抱怨,因为 IDataReader
有您未指定的其他属性。您要么需要将它们全部映射(在本例中显然不需要),要么指定应忽略它们,例如:
CreateMap<IDataReader, EngineModel>()
.ForMember(...)
.ForMember(...)
.ForMember(...)
.ForAllOtherMembers(x => x.Ignore()); // < -- Add this line
我使用的是与您相同的版本,所以这就是我配置自动映射器的方式
在我的startup.cs中我只需要这个
services.AddAutoMapper(config => config.ValidateInlineMaps = false);
以及我如何创建个人资料class
public class CommentProfile : Profile
{
public CommentProfile()
{
CreateMap<Comment, CommentDto>(MemberList.None).ReverseMap();
}
}
public class Comment : BaseEntity
{
public string Content { get; set; }
public virtual Comment ParentComment { get; set; }
public virtual Post Post { get; set; }
public virtual User? User { get; set; }
public CommentStatus CommentStatus { get; set; }
}
public class CommentDto
{
public int Id { get; set; }
public Guid UniqeId { get; set; }
public string Content { get; set; }
public Comment ParentComment { get; set; }
public CommentStatus CommentStatus { get; set; }
public DateTime DateCreated { get; set; }
}
您只需要:
services.AddAutoMapper();
在服务集合中注册 IMapper
并自动应用命名空间中的所有配置文件(从 Profile
继承的任何 class)。
那么,你应该注入 IMapper
,而不是使用静态 Mapper
。在您的控制器中:
public class MyController : Controller
{
private readonly IMapper _mapper;
public MyController(IMapper mapper)
{
_mapper = mapper;
}
然后,当你想映射一些东西时:
var enginesData = _mapper.Map<List<EngineModel>>(dataReader.mcDataReader);
但是,您不能直接将 IDataReader
实例映射到列表,这是您的主要问题。您需要枚举数据 reader 将行存储为列表中的项目,然后您可以从该列表映射到您的 List<EngineModel>
。不过,在这一点上,将行读出为 EngineModel
并在枚举数据 reader 时构建列表可能更有意义。那就根本不用贴图了
您只需要从 NuGet 包安装 AutoMapper.Data(您的情况是 3.0.0),然后在启动时添加以下代码:
services.AddAutoMapper(cfg =>
{
cfg.AddDataReaderMapping();
});
参考
https://github.com/AutoMapper/AutoMapper.Extensions.Microsoft.DependencyInjection/issues/50
我无法在 .net core 2.2 中使用 Auto Mapper。它一直抛出这个错误:
Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters ======================================================================================================================================================================================================= AutoMapper created this type map for you, but your types cannot be mapped using the current configuration. IDataReader -> List
1 (Destination member list) System.Data.IDataReader -> System.Collections.Generic.List
1[[Models.EngineModel, Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] (Destination member list)Unmapped properties: Capacity
这是我的引擎模型 class:
public class EngineModel
{
public string Id { get; set; }
public int engineNo { get; set; }
public string engineHost { get; set; }
}
这是我的个人资料:
public class EngineMappingProfile : Profile
{
public EngineMappingProfile()
{
CreateMap<IDataReader, EngineModel>()
.ForMember(dest => dest.Id , opt => opt.MapFrom(src => src.GetString(src.GetOrdinal("ID"))))
.ForMember(dest => dest.engineNo , opt => opt.MapFrom(src => src.GetInt32(src.GetOrdinal("ENG_NO"))))
.ForMember(dest => dest.engineHost , opt => opt.MapFrom(src => src.GetString(src.GetOrdinal("ENG_HOST"))))
}
}
这是我的 Startup.cs ConfigureServices 方法:
public void ConfigureServices(IServiceCollection services)
{
Mapper.Initialize(cfg =>
{
cfg.AddProfile<EngineMappingProfile>();
});
services.AddAutoMapper();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(options =>
{
options.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});
}
以上所有代码都在我的 API 项目中。我在我的商业项目中使用这些配置文件:
var enginesData = AutoMapper.Mapper.Map<IDataReader, List<EngineModel>>(dataReader.mcDataReader);
我正在使用 "AutoMapper.Extensions.Microsoft.DependencyInjection (6.0.0)" 和“AutoMapper (8.0.0)
AutoMapper 正在抱怨,因为 IDataReader
有您未指定的其他属性。您要么需要将它们全部映射(在本例中显然不需要),要么指定应忽略它们,例如:
CreateMap<IDataReader, EngineModel>()
.ForMember(...)
.ForMember(...)
.ForMember(...)
.ForAllOtherMembers(x => x.Ignore()); // < -- Add this line
我使用的是与您相同的版本,所以这就是我配置自动映射器的方式
在我的startup.cs中我只需要这个
services.AddAutoMapper(config => config.ValidateInlineMaps = false);
以及我如何创建个人资料class
public class CommentProfile : Profile
{
public CommentProfile()
{
CreateMap<Comment, CommentDto>(MemberList.None).ReverseMap();
}
}
public class Comment : BaseEntity
{
public string Content { get; set; }
public virtual Comment ParentComment { get; set; }
public virtual Post Post { get; set; }
public virtual User? User { get; set; }
public CommentStatus CommentStatus { get; set; }
}
public class CommentDto
{
public int Id { get; set; }
public Guid UniqeId { get; set; }
public string Content { get; set; }
public Comment ParentComment { get; set; }
public CommentStatus CommentStatus { get; set; }
public DateTime DateCreated { get; set; }
}
您只需要:
services.AddAutoMapper();
在服务集合中注册 IMapper
并自动应用命名空间中的所有配置文件(从 Profile
继承的任何 class)。
那么,你应该注入 IMapper
,而不是使用静态 Mapper
。在您的控制器中:
public class MyController : Controller
{
private readonly IMapper _mapper;
public MyController(IMapper mapper)
{
_mapper = mapper;
}
然后,当你想映射一些东西时:
var enginesData = _mapper.Map<List<EngineModel>>(dataReader.mcDataReader);
但是,您不能直接将 IDataReader
实例映射到列表,这是您的主要问题。您需要枚举数据 reader 将行存储为列表中的项目,然后您可以从该列表映射到您的 List<EngineModel>
。不过,在这一点上,将行读出为 EngineModel
并在枚举数据 reader 时构建列表可能更有意义。那就根本不用贴图了
您只需要从 NuGet 包安装 AutoMapper.Data(您的情况是 3.0.0),然后在启动时添加以下代码:
services.AddAutoMapper(cfg =>
{
cfg.AddDataReaderMapping();
});
参考
https://github.com/AutoMapper/AutoMapper.Extensions.Microsoft.DependencyInjection/issues/50