【问题标题】:Automapper - Unable to map nested objects/collecionsAutomapper - 无法映射嵌套对象/集合
【发布时间】:2017-06-11 13:40:44
【问题描述】:

我在这里和 automapper wiki 尝试了许多示例,但我仍然无法解决此问题。我正在尝试映射嵌套对象和嵌套集合,无论我做什么,它总是会引发错误。让控制器返回数据的唯一方法是为这两个属性打开 option.ignore。

这些是我要映射的业务层对象

public class LocationBL
{
    public int Id { get; set; }        
    public string Name { get; set; }
    public string Address { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zipcode { get; set; }
    public string Country { get; set; }

    public DbGeography Coordinates { get; set; }

    public int LocationType_Id { get; set; }

    public virtual LocationTypeBL LocationType { get; set; }

    public virtual ICollection<SportBL> Sports { get; set; }
}

public class LocationTypeBL
{
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<LocationBL> Locations { get; set; }
}
public class SportBL
{

    public int Id { get; set; }

    public string Name { get; set; }

    public virtual ICollection<LocationBL> Locations { get; set; }

    public virtual ICollection<UserBL> Users { get; set; }
}

这些是数据层对象

public class Location : EntityData
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [ForeignKey("Company")]
    public int? CompanyId { get; set; }

    [Required]
    public string Name { get; set; }
    public string Address { get; set; }
    public string Address2 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zipcode { get; set; }
    public string Country { get; set; }
    [Required]
    public DbGeography Coordinates { get; set; }
    [ForeignKey("LocationType")]
    public int LocationType_Id { get; set; }

    public virtual LocationType LocationType { get; set; }

    public virtual ICollection<Sport> Sports { get; set; }
    public virtual Company Company { get; set; }
}

public class LocationType : EntityData
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }

    public virtual ICollection<Location> Locations { get; set; }
}

public class Sport : EntityData
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }

    public virtual ICollection<Location> Locations { get; set; }

    public virtual ICollection<User> Users { get; set; }
}

这是我的映射配置文件

public class LocationProfile : Profile
{
    public LocationProfile()
    {
        CreateMap<LocationType, LocationTypeBL>();
        CreateMap<LocationTypeBL, LocationType>();
        CreateMap<Location, LocationBL>()
            .ForMember(Dest => Dest.Sports,
            opt => opt.MapFrom(src => src.Sports))
            .ForMember(Dest => Dest.LocationType,
            opt => opt.MapFrom(src => src.LocationType));

        CreateMap<LocationBL, Location>()
            .ForMember(Dest => Dest.Sports,
            opt => opt.MapFrom(src => src.Sports))
            .ForMember(Dest => Dest.LocationType,
            opt => opt.MapFrom(src => src.LocationType));




    }
}

更新 ******* 这是我的 LocationType 个人资料

public class LocationTypeProfile : Profile
{
    public LocationTypeProfile()
    {
        CreateMap<LocationType, LocationTypeBL>();
        CreateMap<LocationTypeBL, LocationType>();
    }
}

这是我的运动资料

    public class SportProfile : Profile
{
    public SportProfile()
    {
        CreateMap<Sport, SportBL>();
        CreateMap<SportBL, Sport>();
    }
}

不确定是否重要,但这是使用 Autofac、WebAPI 和 OWIN 的 Azure 移动应用后端。这是我第一次使用 AutoMapper 和 Autofac,所以请原谅我,因为我还在学习。配置文件都已注册,如果我将嵌套对象设置为忽略,控制器会返回正确的数据。

提前谢谢你!!!

【问题讨论】:

    标签: c# azure nested inversion-of-control automapper


    【解决方案1】:

    你快到了。您还需要指导 AutoMapper 如何映射嵌套对象。所以你需要为SportSportBL 创建一个映射,反之亦然。

    // use ForMember if needed, but you know how to do that so I won't
    // show it.
    CreateMap<Sport, SportBL>(); 
    

    然后 AutoMapper 将在映射嵌套复杂类型时使用该映射。

    另外注意,如果你的类有相同的属性,你可以调用ReverseMap()方法,它会为你做双向映射。

    所以不要这样:

    CreateMap<LocationType, LocationTypeBL>();
    CreateMap<LocationTypeBL, LocationType>();
    

    你可以这样做来完成同样的事情:

    Mapper.CreateMap<LocationType, LocationTypeBL>().ReverseMap();
    

    【讨论】:

    • 另外,我最初尝试使用 .ReverseMap() 但它不起作用。
    • UserUserBL 怎么样,你有他们的映射吗?还有什么例外。完整的堆栈跟踪将有所帮助。
    • User 和 UserBL 已映射。
    • 这是异常 AutoMapper.AutoMapperMappingException 错误映射类型。映射类型:Location -> LocationBL XXXXX.DAL.DataObjects.Location -> XXXXX.Business.DataObjects.LocationBL 类型映射配置:Location -> LocationBL XXXXX.DAL.DataObjects.Location -> XXXXXX.Business.DataObjects.LocationBL 属性:LocationType当我忽略 LocationType 时,我在 Sports 上得到完全相同的错误。
    • 我找到了根本原因。该服务返回一个 iqueryable 而不是 ienumerable。现在有一个新问题。由于某种原因,嵌套的集合/对象需要永远运行并最终导致 502 错误。我会将您的答案标记为正确,并为此提出一个需求问题。谢谢
    猜你喜欢
    • 2017-07-20
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 2022-01-06
    • 2020-04-03
    • 1970-01-01
    相关资源
    最近更新 更多