帕特里克(Patrick),感谢您提供有关正确问题的建议!
Patrick, thanks for advice about correct question!
我有三个表,用于多对多关系.像这样:
I have three table for many to many relationship. Like this:
GoodEntity:
GoodEntity:
public partial class GoodEntity { public GoodEntity() { this.GoodsAndProviders = new HashSet<GoodAndProviderEntity>(); } public int id { get; set; } public string name { get; set; } public string description { get; set; } public decimal cost { get; set; } public Nullable<decimal> price { get; set; } public virtual ICollection<GoodAndProviderEntity> GoodsAndProviders { get; set; } }ProviderEntity:
ProviderEntity:
public partial class ProviderEntity { public ProviderEntity() { this.GoodsAndProviders = new HashSet<GoodAndProviderEntity>(); } public int id { get; set; } public string name { get; set; } public string description { get; set; } public string address { get; set; } public string phone { get; set; } public string email { get; set; } public string url { get; set; } public Nullable<int> rating { get; set; } public virtual ICollection<GoodAndProviderEntity> GoodsAndProviders { get; set; } }多对多关系实体:
public partial class GoodAndProviderEntity { public int id { get; set; } public int good_id { get; set; } public int provider_id { get; set; } public virtual GoodEntity Goods { get; set; } public virtual ProviderEntity Providers { get; set; } }GoodDTO:
public class GoodDTO { public int id { get; set; } public string name { get; set; } public string description { get; set; } public decimal cost { get; set; } public decimal? price { get; set; } public IList<ProviderDTO> providers { get; set; } }ProviderDTO:
ProviderDTO:
public class ProviderDTO { public int id { get; set; } public string name { get; set; } public string description { get; set; } public string address { get; set; } public string phone { get; set; } public string email { get; set; } public string url { get; set; } public int? rating { get; set; } }这是创建地图的代码:
Mapper.CreateMap<ProviderDTO, ProviderEntity>(); Mapper.CreateMap<ProviderEntity, ProviderDTO>(); Mapper.CreateMap<GoodEntity, GoodDTO>() .ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders)); Mapper.CreateMap<GoodAndProviderEntity, ProviderDTO>();它工作一半. Automapper完全映射为商品",并为此商品的所有提供者创建了清单.但是自动映射器无法填充提供程序.
And it works half. Automapper was mapped "goods" completely and was created list for all providers for this goods. But automapper don`t fill providers.
如果我使用Mapper.AssertConfigurationIsValid(),则:
If I use Mapper.AssertConfigurationIsValid(), then:
找到未映射的成员.在下面查看类型和成员.添加自定义映射表达式,忽略,添加自定义解析器或修改源/目标类型==================================================================================================================================================================================== ========================== ProviderDTO-> ProviderEntity(目标成员列表)Core.DTO.ProviderDTO-> DAL.EF.Entities.ProviderEntity(Destination成员列表)未映射的属性:GoodsAndProviders ========================================== ==================== AndAndProviderEntity-> ProviderDTO(目标成员列表)DAL.EF.Entities.GoodAndProviderEntity-> Core.DTO.ProviderDTO(目标成员列表)
Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type ======================================================= ProviderDTO -> ProviderEntity (Destination member list) Core.DTO.ProviderDTO -> DAL.EF.Entities.ProviderEntity (Destination member list) Unmapped properties: GoodsAndProviders ============================================================== GoodAndProviderEntity -> ProviderDTO (Destination member list) DAL.EF.Entities.GoodAndProviderEntity -> Core.DTO.ProviderDTO (Destination member list)
如何为多对多关系创建映射?
How to create mapping for many-to-many relationship?
关于安东,
推荐答案使用您当前的代码,您正在尝试将GoodAndProviderEntity映射到ProviderDTO.
With your current code you're trying to map the GoodAndProviderEntity into ProviderDTO.
Mapper.CreateMap<GoodEntity, GoodDTO>() .ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders));您要做的是将ProviderEntity映射到ProviderDTO中,因此您要做的就是从GoodsAndProviders中选择Providers作为列表:
What you want to do, is to map ProviderEntity into ProviderDTO, so all you have to do is select the Providers from GoodsAndProviders as a list:
Mapper.CreateMap<GoodEntity, GoodDTO>() .ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders.Select(y => y.Providers).ToList()));更多推荐
自动映射器多对多映射
发布评论