Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automapper many to many mapping

Patrick, thanks for advice about correct question!

EDIT 1:

I have three table for many to many relationship. Like this: 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; }
}

Entity for many-to-many relationship:

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; }
}

This is code for creation maps:

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>();

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. enter image description here

If I use Mapper.AssertConfigurationIsValid(), then:

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?

Regards, Anton

like image 637
Hellaren Avatar asked Feb 09 '15 10:02

Hellaren


People also ask

Can AutoMapper map enums?

The built-in enum mapper is not configurable, it can only be replaced. Alternatively, AutoMapper supports convention based mapping of enum values in a separate package AutoMapper.

Is AutoMapper faster than manual mapping?

Inside this article, it discusses performance and it indicates that Automapper is 7 times slower than manual mapping.

Is AutoMapper bidirectional?

What is AutoMapper Reverse Mapping in C#? The Automapper Reverse Mapping is nothing but the two-way mapping which is also called as bidirectional mapping.


1 Answers

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));

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()));
like image 137
Sitl Avatar answered Sep 23 '22 18:09

Sitl