gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:42:02 33 4
gpt4 key购买 nike

我的实体

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.dll 中发生了 'AutoMapper.AutoMapperMappingException' 类型的异常,但未在用户代码中处理

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

我使用 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));
});

而不是两次调用。

关于c# - 如何解决 "exception of type ' AutoMapper.AutoMapperMappingException' occurred in AutoMapper"- "Error mapping types"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38979781/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com