【发布时间】:2017-08-21 18:00:56
【问题描述】:
我正在尝试将我的一个模型的嵌套 ICollection 映射到现有 Dto,但我正在努力使用 AutoMapper 正确映射它
型号:
public class Ingredient : BaseEntity<long>
{
[MaxLength(100)]
public string Name { get; set; }
[ForeignKey("Id")]
public int CustomerId { get; set; }
public bool IsPackaging { get; set; }
public virtual ICollection<ProductIngredient> ProductIngredient { get; set; }
public virtual ICollection<IngredientComposition> IngredientComposition { get; set; }
}
收藏模型:
public class IngredientComposition : BaseEntity<int>
{
[MaxLength(20)]
public string Type { get; set; }
[MaxLength(200)]
public string Key { get; set; }
[MaxLength(200)]
public string Value { get; set; }
}
Dto:
public class IngredientDto
{
public long Id { get; set; }
public DateTime CretedOn { get; set; }
public DateTime UpdatedOn { get; set; }
public string Name { get; set; }
public int CustomerId { get; set; }
public int UsedCount { get; set; }
public bool IsPackaging { get; set; }
public IList<Composition> Ingredients { get; set; }
}
public class Composition
{
public string Type { get; set; }
public string Key { get; set; }
public string Value { get; set; }
}
当我努力正确设置“ForMemeber”方法时,我的地图如下所示:
CreateMap<Ingredient, IngredientDto>();
CreateMap<IngredientDto, Ingredient>();
任何帮助都非常合适! 谢谢
编辑:
这就是我获取数据的方式:
return await _context.Ingredients
.Where(i => i.CustomerId ==_userResolverService.GetCustomerId())
.Include(i => i.IngredientComposition)
.Select(i => _mapper.Map<Ingredient, IngredientDto>(i))
.OrderBy(i => i.Name)
.ToListAsync();
【问题讨论】:
-
添加
CreateMap<IngredientComposition, Composition>();并重试。 -
您好,不幸的是,这不起作用。我仍然在 IngredientDto 上得到 null 而不是数组,但成分已附加集合 - 看看编辑我如何获取数据
标签: c# entity-framework .net-core automapper