How to map value in linking table

19 views
Skip to first unread message

AutoMUser

unread,
Jun 9, 2017, 4:26:37 AM6/9/17
to AutoMapper-users
Hi


I add an extra Attribute in the linking table 

public partial class GoodAndProviderEntity
{
   
public int id { get; set; }
   
public int good_id { get; set; }
   
public int provider_id { get; set; }
   
public string Value { get; set; }             <----- NEW


   
public virtual GoodEntity Goods { get; set; }
   
public virtual ProviderEntity Providers { get; set; }
}


And then I add an extra attribute in the dto class


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 string Value {get;set;}              <--- NEW


   
public IList<ProviderDTO> providers { get; set; }
}


How do I map this value to the dto (and the other way around)?

I tried 

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


But it doesnt work



Jimmy Bogard

unread,
Jun 9, 2017, 9:04:16 AM6/9/17
to automapper-users
Make sure your DTOs have a similar shape to your entities. It looks like you're trying to skip the join table entity on the DTO side - don't.

If you MUST not have that join table, map like this:

Entity -> Intermediate Model -> DTO

Make it a 2-step process.

On the reverse side, no idea. I don't like to model my DTOs like this for many-to-many.

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages