我正在研究一种解决方案,该解决方案需要将 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());
我会感谢你的帮助