自动映射多对多映射

时间:2015-02-09 10:17:03

标签: c# .net entity-framework automapper

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

编辑1:

我有三张关于多对多关系的表。像这样: EF data model

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不会填补提供者。 enter image description here

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

  

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

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

问候,安东

1 个答案:

答案 0 :(得分:14)

使用您当前的代码,您尝试将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()));
相关问题