Automapper 链/嵌套 object 映射抛出异常
Automapper chain / nested object mapping throw exception
我在 .NET CORE 5 项目中使用 AutoMapper 版本 10.1.1。我嵌套了 object 我需要映射但无法这样做并得到以下异常,尽管我可以获得第一个 child 的数据映射,在我的情况下 'Enforcements' 但无法访问 child child Object 'Contraventions'
Class 级别 0
public class SiteContraventionDataView
{
public int SiteId { get; set; }
public string SiteName { get; set; }
public ICollection<EnforcementDataView> Enforcements { get; set; }
}
Child Class 级别 1
public class EnforcementDataView
{
public int EnforcementId { get; set; }
public ICollection<ContraventionDataView> Contraventions { get; set; }
}
当我尝试从 child class 上方为 'Contraventions' 设置源时出现错误。我正在尝试使用 lambda 表达式来获取 Contravention object 参考
.ForMember(dataView => dataView.Enforcements.Select(x=>x.Contraventions), opt => opt.MapFrom(dataModel => GetContraventions(dataModel.Enforcements.ToList())))
自动映射class
public class SiteContraventionsProfile : Profile
{
public SiteContraventionsProfile()
{
CreateMap<Site, SiteContraventionDataView>()
.ForMember(dataView => dataView.SiteId, opt => opt.MapFrom(dataModel => dataModel.SiteId))
.ForMember(dataView => dataView.SiteName, opt => opt.MapFrom(dataModel => dataModel.SiteName))
.ForMember(dataView => dataView.Enforcements, opt => opt.MapFrom(dataModel => dataModel.Enforcements))
.ForMember(dataView => dataView.Enforcements.Select(x=>x.Contraventions), opt => opt.MapFrom(dataModel => GetContraventions(dataModel.Enforcements.ToList())))
;
}
private List<Contravention> GetContraventions(List<Enforcement> enforcements)
{
List<Contravention> contraventions = new List<Contravention>();
if (enforcements.Any())
{
var t1 = enforcements.Select(x => x.EnforcementContraventions);
}
return contraventions;
}
}
尝试为 Enforcement
和 Contravention
添加映射:
CreateMap<Enforcement, EnforcementDataView>();
CreateMap<Contravention, ContraventionDataView>();
CreateMap<Site, SiteContraventionDataView>();
我在 .NET CORE 5 项目中使用 AutoMapper 版本 10.1.1。我嵌套了 object 我需要映射但无法这样做并得到以下异常,尽管我可以获得第一个 child 的数据映射,在我的情况下 'Enforcements' 但无法访问 child child Object 'Contraventions'
Class 级别 0
public class SiteContraventionDataView
{
public int SiteId { get; set; }
public string SiteName { get; set; }
public ICollection<EnforcementDataView> Enforcements { get; set; }
}
Child Class 级别 1
public class EnforcementDataView
{
public int EnforcementId { get; set; }
public ICollection<ContraventionDataView> Contraventions { get; set; }
}
当我尝试从 child class 上方为 'Contraventions' 设置源时出现错误。我正在尝试使用 lambda 表达式来获取 Contravention object 参考
.ForMember(dataView => dataView.Enforcements.Select(x=>x.Contraventions), opt => opt.MapFrom(dataModel => GetContraventions(dataModel.Enforcements.ToList())))
自动映射class
public class SiteContraventionsProfile : Profile
{
public SiteContraventionsProfile()
{
CreateMap<Site, SiteContraventionDataView>()
.ForMember(dataView => dataView.SiteId, opt => opt.MapFrom(dataModel => dataModel.SiteId))
.ForMember(dataView => dataView.SiteName, opt => opt.MapFrom(dataModel => dataModel.SiteName))
.ForMember(dataView => dataView.Enforcements, opt => opt.MapFrom(dataModel => dataModel.Enforcements))
.ForMember(dataView => dataView.Enforcements.Select(x=>x.Contraventions), opt => opt.MapFrom(dataModel => GetContraventions(dataModel.Enforcements.ToList())))
;
}
private List<Contravention> GetContraventions(List<Enforcement> enforcements)
{
List<Contravention> contraventions = new List<Contravention>();
if (enforcements.Any())
{
var t1 = enforcements.Select(x => x.EnforcementContraventions);
}
return contraventions;
}
}
尝试为 Enforcement
和 Contravention
添加映射:
CreateMap<Enforcement, EnforcementDataView>();
CreateMap<Contravention, ContraventionDataView>();
CreateMap<Site, SiteContraventionDataView>();