我的实体
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);
类型&#39; AutoMapper.AutoMapperMappingException&#39;的例外情况发生在AutoMapper.dll中但未在用户代码中处理
其他信息:错误映射类型。
我使用AutoMapper 5.1.1,Visual Studio 2015和MongoDB
请帮帮我!!谢谢!
答案 0 :(得分:1)
您只能调用一次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));
});
而不是两次电话。