How to map a POCO with a Dictionary and an external parameter to a new EF model

81 Views Asked by At

I'm working on a solution that needs to convert a POCO to a concrete data model I'm using with Entity Framework Code First.

The Original POCO looks like this:

public class Original
{
    public string Name { get; set; }
    public string Test { get; set; }
    public Dictionary<string, string> Keys { get; set; }
}

The Destination EF Data Model class look like this:

public class Destination
{
    public string Name { get; set; } //Must be filled with the original Name field
    public string Test { get; set; } //Must be filled with the original Test field
    public virtual ICollection<Detail> Details { get; set; } //Must be filled with the Original Dictionary field
    public virtual State State { get; set; } //Can be ignored
}

public class Detail
{
    public string Key { get; set; }
    public string Value { get; set; }
    public int Id { get; set; } //Must be filled with an external parameter
    public virtual Info Info { get; set; } //Can be ignored
}

And this is what I got at this time:

var param = 0; //This value must be passed to the Detail.Id field of each entity

var result = _mapper.Map<Destination>(original); //How to pass the param variable to the Map method?

cfg.CreateMap<Original, Destination>()
    .ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Name))
    .ForMember(dest => dest.Test, opt => opt.MapFrom(src => src.Test))
    //.ForMember(dest => dest.Details, opt => opt.MapFrom(src => src.Keys)) //No idea what to do here
    .ForMember(dest => dest.State, opt => opt.Ignore());

I will appreciate your help

0

There are 0 best solutions below