自动映射多对多映射

Hel*_*ren 7 .net c# entity-framework automapper

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

编辑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; }
}
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)

多对多关系的实体:

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; }
}
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)

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; }
}
Run Code Online (Sandbox Code Playgroud)

这是创建地图的代码:

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>();
Run Code Online (Sandbox Code Playgroud)

它的工作量减半.Automapper已完全映射到"商品",并为此商品的所有提供商创建了列表.但是,automapper不会填补提供者. 在此输入图像描述

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

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

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

此致,安东

小智 17

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

Mapper.CreateMap<GoodEntity, GoodDTO>()
  .ForMember(dto => dto.providers, opt => opt.MapFrom(x => x.GoodsAndProviders));
Run Code Online (Sandbox Code Playgroud)

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

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

  • 如果我要进行多对多连接,并且还有存储在 GoodAndProviderEntity 中的附加字段信息,例如 GoodAndProviderEntity.IsMapped 您知道 AutoMapper 如何将该值从中间的多对多字段传递到 GoodDTO 上吗? (2认同)
  • @ goodies4uall您是否找到相反的方法?我在寻找同一件事 (2认同)
  • 我的缺点是我没有包含所有的表,所以这会给我列表 [ null, null] ,其中包含 null 值,请务必包含并然后包含两个表。否则这就像一个魅力 (2认同)