将列表<User>映射到列表<userReponseDto>



如何使用自动映射器将身份用户列表映射到列表 dto?

我很难让我的自动映射器将身份用户列表映射到 dto。我拉取了用户列表,长度> 0,但是当我映射它时,它变成了一个空列表。这是我的代码。

启动.cs

 public void ConfigureServices(IServiceCollection services)
        {
     var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new MappingProfile());
            });
     IMapper mapper = mappingConfig.CreateMapper();
            services.AddSingleton(mapper);
}

映射配置文件.cs

public class MappingProfile : Profile
    {
        public MappingProfile()
        {
            // Add as many of these lines as you need to map your objects
            CreateMap<User, userReponseDto>()
            .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id));
            ;
            CreateMap<List<User>, IEnumerable<userReponseDto>>();
            CreateMap<List<User>, List<userReponseDto>>();
        }
    }

userReponseDto.cs

public class userReponseDto
    {
        public string Id { get; set; }
        public DateTimeOffset? LockoutEnd { get; set; }
        //
        // Summary:
        //     Gets or sets a flag indicating if two factor authentication is enabled for this
        //     user.
        public bool TwoFactorEnabled { get; set; }
        //
        // Summary:
        //     Gets or sets a flag indicating if a user has confirmed their telephone address.
        public bool PhoneNumberConfirmed { get; set; }
        //
        // Summary:
        //     Gets or sets a telephone number for the user.
        public string PhoneNumber { get; set; }
        //
        // Summary:
        //     A random value that must change whenever a user is persisted to the store
        public string ConcurrencyStamp { get; set; }
        // Summary:
        //     Gets or sets a flag indicating if a user has confirmed their email address.
        public bool EmailConfirmed { get; set; }
        // Summary:
        //     Gets or sets the email address for this user.
        public string Email { get; set; }
        // Summary:
        //     Gets or sets the user name for this user.
        public string UserName { get; set; }
        // Summary:
        //     Gets or sets a flag indicating if the user could be locked out.
        public bool LockoutEnabled { get; set; }
        //
        // Summary:
        //     Gets or sets the number of failed login attempts for the current user.
        public int AccessFailedCount { get; set; }
    }

用户.cs

public class User : IdentityUser
    {
    }

ApplicationDbContext.cs

        public DbSet<User> ApplicationUsers { get; set; }

用户控制器.cs

    public class UsersController : ControllerBase{
            private readonly ApplicationDbContext _context;
            private readonly IMapper _mapper;
            public UsersController(IMapper mapper,ApplicationDbContext context)
        {
            _mapper = mapper;
            _context = context;
        }
 [HttpPost]
        public IActionResult GetUsers()
        {
                    // return Ok(_context.ApplicationUsers.ToList());
                    var list = _context.ApplicationUsers.ToList();
                    IEnumerable<userReponseDto> userList ;
                    var response = _mapper.Map<List<userReponseDto>>(list);
                    // var response =
                    userList= list.Select(_mapper.Map<IdentityUser, userReponseDto>);
                    return Ok(userList);
        }
}

我能够确定您不应该根据自动映射器映射列表变为 0 @darin-dimitrov 来定义每个列表的映射。我认为这是由于身份用户中的嵌套属性。所以我的映射配置文件变成了。

        CreateMap<User, userReponseDto>()
           .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id));
        CreateMap<IdentityUser, userReponseDto>()
           .ForMember(dest => dest.Id, opt => opt.MapFrom(src => src.Id));

未声明列表映射。然后

var list = _context.ApplicationUsers.ToList();
var response = _mapper.Map<List<userReponseDto>>(list);

现在工作正常。

相关内容

  • 没有找到相关文章

最新更新