我在映射时遇到麻烦。当我尝试映射模型时,出现了下一个错误:
错误映射类型。
映射类型:TeamModel-> Team Basketball.Core.Models.TeamModel-> Basketball.Core.Entities.Team
类型图配置:TeamModel-> Team Basketball.Core.Models.TeamModel-> Basketball.Core.Entities.Team
目标成员:位置
我尝试绘制下一个模型:
public class TeamModel
{
public string Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public string Region { get; set; }
public string Country { get; set; }
public bool InPlayoff { get; set; }
public int Wins { get; set; }
public int Loses { get; set; }
}
public class Team
{
public int Id { get; set; }
public string Name { get; set; }
public int LocationId { get; set; }
public bool InPlayoff { get; set; }
public int Wins { get; set; }
public int Loses { get; set; }
public Location Location { get; set; }
}
以及我的映射器配置:
CreateMap<TeamModel, Team>()
.ForMember(x => x.Id, x => x.MapFrom(y => y.Id))
.ForMember(x => x.Name, x => x.MapFrom(y => y.Name))
.ForMember(x => x.InPlayoff, x => x.MapFrom(y => y.InPlayoff))
.ForMember(x => x.Wins, x => x.MapFrom(y => y.Wins))
.ForMember(x => x.Loses, x => x.MapFrom(y => y.Loses));
我不需要映射位置,区域和国家,但是如果我添加:
.ForPath(x => x.Location.Name, x => x.MapFrom(y => y.Location))
.ForPath(x => x.Location.Region, x => x.MapFrom(y => y.Region))
.ForPath(x => x.Location.Country, x => x.MapFrom(y => y.Country))
映射器正在运行,但是我说过不需要。如何解决这个错误?我在哪里弄错了?
您可以这样忽略此属性:
CreateMap<TeamModel, Team>()
.ForMember(x => x.Id, x => x.MapFrom(y => y.Id))
.ForMember(x => x.Name, x => x.MapFrom(y => y.Name))
.ForMember(x => x.InPlayoff, x => x.MapFrom(y => y.InPlayoff))
.ForMember(x => x.Wins, x => x.MapFrom(y => y.Wins))
.ForMember(x => x.Loses, x => x.MapFrom(y => y.Loses))
.ForMember(x => x.Location, opt => opt.Ignore());