如何将POCO与Dictionary和外部参数映射到新的EF模型

时间:2017-12-07 22:23:54

标签: c# entity-framework automapper automapper-6

我正在研究一种解决方案,需要将POCO转换为我正在使用Entity Framework Code First的具体数据模型。

原始POCO看起来像这样:

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

目标EF数据模型类如下所示:

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
}

这就是我此时得到的:

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

我将非常感谢您的帮助

0 个答案:

没有答案