自动映射 - 无法映射嵌套对象/集合

时间:2017-01-26 05:31:54

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

我在这里尝试了很多例子,并且从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,所以请原谅我,因为我还在学习。配置文件都已注册,如果我将嵌套对象设置为忽略,控制器将返回正确的数据。

提前谢谢!!!

1 个答案:

答案 0 :(得分: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();