我正在使用.Net Core中的Automapper(automapper.extensions.microsoft.dependencyinjection)从模型列表映射到dto。我已经使用ForMember为属性创建了一个映射,但是却不断收到一个错误,指出该属性未映射。
services.AddAutoMapper(typeof(Startup).Assembly);
var userRolesDtos =
_mapper.Map<IEnumerable<UserRole>, IEnumerable<UserRoleDto>>(await _userRoleService.GetUserRolesAsync(urrp)); // method return <IEnumerable<UserRole>
public class UserRole
{
[Key]
[Required]
public Guid Id { get; set; }
[Required]
public string Name { get; set; }
public ICollection<UserRolePermission> UserRolePermissions { get; set; } = new List<UserRolePermission>();
public DateTime? DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public DateTime? DateDeleted { get; set; }
}
public class UserRoleDto
{
public Guid Id { get; set; }
public string Name { get; set; }
public DateTime? DateCreated { get; set; }
public DateTime? DateModified { get; set; }
public DateTime? DateDeleted { get; set; }
public ICollection<UserPermissionDto> UserPermissions { get; set; } = new List<UserPermissionDto>();
}
public class UserRoleProfile : Profile
{
public UserRoleProfile()
{
CreateMap<UserRole, UserRoleForCreation>().ReverseMap();
CreateMap<UserRole, UserRoleForPartialUpdate>().ReverseMap();
CreateMap<UserRole, UserRoleDto>(MemberList.None)
.ForMember(
dest => dest.UserPermissions,
src => src.Ignore()
);
}
}
我希望该对象能够被映射,并且UserPermissions属性将被忽略,而是出现此错误:
{AutoMapper.AutoMapperConfigurationException: 找到未映射的成员。在下面查看类型和成员。 添加自定义映射表达式,忽略,添加自定义解析器或修改源/目标类型
AutoMapper为您创建了此类型映射,但是无法使用当前配置来映射您的类型。 UserRole-> UserRoleDto(目标成员列表) BIN.Core.Models.UserRoles.UserRole-> BIN.Core.Dtos.UserRoleDto(目标成员列表)
未映射的属性: 用户权限
在AutoMapper.ConfigurationValidator.AssertConfigurationIsValid(IEnumerable 1 typeMaps) in C:\projects\automapper\src\AutoMapper\ConfigurationValidator.cs:line 48
at lambda_method(Closure , IEnumerable
1,IEnumerable`1,ResolutionContext)}