自动映射器多对多映射

编程入门 行业动态 更新时间:2024-10-27 09:48:16
本文介绍了自动映射器多对多映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

帕特里克(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()));

更多推荐

自动映射器多对多映射

本文发布于:2023-11-04 12:06:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1557892.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:映射器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!