gpt4 book ai didi

c# - Automapper - 无法映射嵌套对象/集合

转载 作者:行者123 更新时间:2023-12-02 06:48:48 24 4
gpt4 key购买 nike

我已经尝试了这里和 automapper wiki 中的大量示例,但仍然无法解决此问题。我正在尝试映射嵌套对象和嵌套集合,无论我做什么,它总是会抛出错误。让 Controller 返回数据的唯一方法是打开这两个属性的 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; }
}

这是我的 map 配置文件

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,所以请原谅我,因为我仍在学习。配置文件均已注册,如果我将嵌套对象设置为忽略, Controller 将返回正确的数据。

提前谢谢您!!!

最佳答案

你就快到了。您还需要指示 AutoMapper 如何映射嵌套对象。因此,您需要为 Sport 创建一个映射到 SportBL,反之亦然。

// 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();

关于c# - Automapper - 无法映射嵌套对象/集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41867458/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com