C# 自动映射-无法映射嵌套对象/集合
我在这里和automapper wiki上尝试了许多例子,但仍然无法解决这个问题。我试图映射一个嵌套对象和一个嵌套集合,无论我做什么,它总是抛出一个错误。使控制器返回数据的唯一方法是为这两个属性启用option.ignore 这些是我试图映射的业务层对象C# 自动映射-无法映射嵌套对象/集合,c#,azure,nested,inversion-of-control,automapper,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 A
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; }
}
公共类位置bl
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共字符串地址{get;set;}
公共字符串地址2{get;set;}
公共字符串City{get;set;}
公共字符串状态{get;set;}
公共字符串Zipcode{get;set;}
公共字符串国家{get;set;}
公共数据库地理坐标{get;set;}
public int LocationType_Id{get;set;}
公共虚拟位置类型BL位置类型{get;set;}
公共虚拟ICollection体育{get;set;}
}
公共类位置类型BL
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection位置{get;set;}
}
公营体育
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection位置{get;set;}
公共虚拟ICollection用户{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; }
}
公共类位置:EntityData
{
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int Id{get;set;}
[外键(“公司”)]
公共int?CompanyId{get;set;}
[必需]
公共字符串名称{get;set;}
公共字符串地址{get;set;}
公共字符串地址2{get;set;}
公共字符串City{get;set;}
公共字符串状态{get;set;}
公共字符串Zipcode{get;set;}
公共字符串国家{get;set;}
[必需]
公共数据库地理坐标{get;set;}
[外键(“位置类型”)]
public int LocationType_Id{get;set;}
公共虚拟位置类型位置类型{get;set;}
公共虚拟ICollection体育{get;set;}
公共虚拟公司公司{get;set;}
}
公共类LocationType:EntityData
{
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection位置{get;set;}
}
公共体育课:EntityData
{
[数据库生成(DatabaseGeneratedOption.Identity)]
公共int Id{get;set;}
[必需]
公共字符串名称{get;set;}
公共虚拟ICollection位置{get;set;}
公共虚拟ICollection用户{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));
}
}
公共类位置配置文件:配置文件
{
公共场所简介()
{
CreateMap();
CreateMap();
CreateMap()
.FormMember(目标=>目标运动,
opt=>opt.MapFrom(src=>src.Sports))
.ForMember(Dest=>Dest.LocationType,
opt=>opt.MapFrom(src=>src.LocationType));
CreateMap()
.FormMember(目标=>目标运动,
opt=>opt.MapFrom(src=>src.Sports))
.ForMember(Dest=>Dest.LocationType,
opt=>opt.MapFrom(src=>src.LocationType));
}
}
更新*******
这是我的位置类型配置文件
public class LocationTypeProfile : Profile
{
public LocationTypeProfile()
{
CreateMap<LocationType, LocationTypeBL>();
CreateMap<LocationTypeBL, LocationType>();
}
}
公共类LocationTypeProfile:Profile
{
公共位置类型配置文件()
{
CreateMap();
CreateMap();
}
}
这是我的运动简介
public class SportProfile : Profile
{
public SportProfile()
{
CreateMap<Sport, SportBL>();
CreateMap<SportBL, Sport>();
}
}
公共级SportProfile:Profile
{
公众体育概况()
{
CreateMap();
CreateMap();
}
}
不确定这是否重要,但这是一个使用Autofac、WebAPI和OWIN的Azure移动应用后端。这是我第一次使用AutoMapper和Autofac,所以请原谅我,因为我还在学习。配置文件都已注册,如果我将嵌套对象设置为忽略,控制器将返回正确的数据
提前谢谢你 你就快到了。您还需要指导AutoMapper如何映射嵌套对象。因此,您需要为
Sport
到SportBL
创建地图,反之亦然
// use ForMember if needed, but you know how to do that so I won't
// show it.
CreateMap<Sport, SportBL>();
您可以这样做来完成同样的事情:
Mapper.CreateMap<LocationType, LocationTypeBL>().ReverseMap();
Mapper.CreateMap().ReverseMap();
另外,我最初尝试使用.ReverseMap(),但它不起作用。关于User
和UserBL
,您是否已将它们映射?还有什么例外呢。完整堆栈跟踪将有所帮助。已映射User和UserBL。这是异常AutoMapper.AutoMappingException错误映射类型。映射类型:Location->LocationBL XXXXX.DAL.DataObjects.Location->XXXXX.Business.DataObjects.LocationBL类型映射配置:Location->LocationBL XXXXX.DAL.DataObjects.Location->XXXXX.Business.DataObjects.LocationBL属性:LocationType当我忽略LocationType时,我在体育上得到了完全相同的错误。我找到了根本原因。服务返回的是iqueryable而不是ienumerable。现在有一个新问题。出于某种原因,嵌套的集合/对象需要永远运行,最终导致502错误。我会将你的答案标记为正确,并为此提出一个必要的问题。非常感谢。
Mapper.CreateMap<LocationType, LocationTypeBL>().ReverseMap();