繁体   English   中英

自动映射多对多映射

[英]Automapper many to many mapping

帕特里克,感谢有关正确问题的建议!

编辑1:

我有三张关于多对多关系的表。 像这样: EF数据模型

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:

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:

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已完全映射到“商品”,并为此商品的所有提供商创建了列表。 但是,automapper不会填补提供者。 在此输入图像描述

如果我使用Mapper.AssertConfigurationIsValid(),那么:

找到了未映射的成员。 查看下面的类型和成员。 添加自定义映射表达式,忽略,添加自定义解析程序或修改源/目标类型============================== ========================= ProviderDTO - > ProviderEntity(目的地成员列表)Core.DTO.ProviderDTO - > DAL.EF.Entities.ProviderEntity(目的地)成员列表)未映射的属性:GoodsAndProviders =========================================== =================== GoodAndProviderEntity - > ProviderDTO(目标成员列表)DAL.EF.Entities.GoodAndProviderEntity - > Core.DTO.ProviderDTO(目标成员列表)

如何为多对多关系创建映射?

此致,安东

使用当前代码,您尝试将GoodAndProviderEntity映射到ProviderDTO。

Mapper.CreateMap<GoodEntity, GoodDTO>()
  .ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders));

你想要做的是将ProviderEntity映射到ProviderDTO,所以你要做的就是从GoodsAndProviders中选择Providers作为列表:

    Mapper.CreateMap<GoodEntity, GoodDTO>()
      .ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders.Select(y => y.Providers).ToList()));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM