我如何解决 "exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper" - "Error mapping types"?

How do I solve "exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper" - "Error mapping types"?

我的实体

public partial class Users
{
    public Users()
    {
        sexs = new HashSet<Ta>();
    }
    [BsonElement("_id")]
    [BsonId]
    public ObjectId UserID { get; set; }
    public string username { get; set; }
    public string email { get; set; }
    public virtual ICollection<Ta> sexs { get; set; }
}
public partial class Ta
{
    public string male { get; set; }
    public string female { get; set; }   
}


public class TEntity
{
    public string male { get; set; }
    public string female { get; set; }
}

 public class UsersEntity 
  {

    public ObjectId UserID { get; set; }
    public string email { get; set; }
    public virtual ICollection<TEntity> sexs { get; set; }
    public string username { get; set; }

}

我的服务:

  public void Insert(UsersEntity student)
       {
            Mapper.Initialize(cfg => cfg.CreateMap<TEntity, Ta>());
            Mapper.Initialize(cfg => cfg.CreateMap<UsersEntity, Users().ForMember(c => c.sexs,o => o.MapFrom(s =>s.sexs)));

            var a = Mapper.Map<UsersEntity, Users>(student);

            _unitOfwork.UsersRepo.Add(a);

        }

我失败了

var a = Mapper.Map<UsersEntity, Users>(student);

类型 'AutoMapper.AutoMapperMappingException' 的异常发生在 AutoMapper.dll 但未在用户代码中处理

附加信息:映射类型错误。

我使用 AutoMapper 5.1.1、Visual Studio 2015 和 MongoDB

请帮帮我!!谢谢!

您只能调用 Mapper.Initialize 一次。每次调用 Initialize 时,首先调用 Mapper.Reset()。尝试 C#

Mapper.Initialize(cfg => {
            cfg.CreateMap<TEntity, Ta>();
            cfg.CreateMap<UsersEntity, Users>().ForMember(c => c.sexs, o => o.MapFrom(s => s.sexs));
            });

而不是两次调用。