AutoMapper加载父对象中的子对象,并在父对象中存储外键

时间:2020-09-16 14:35:45

标签: c# automapper

关于基于父对象中存储的外键在父对象中加载子对象的问题,我对AutoMapper有疑问。这是我的课程的示例:

Class Parent {
   public int ChildIdForeignKey;
   public Child ChildIWantToLoad;
}

Class Child {
   public string notes;
}

Class ParentDto {
   public int ChildIdForeignKey;
   public ChildDto ChildIWantToLoad;
}

Class ChildDto {
   public string notes;
}

我将所有信息存储在数据库中,我想知道是否有可能在加载父级时自动加载父级中指定ID的子级。

我现在有这个,这只是基本映射。

MapperConfiguration mapperConfiguration = new MapperConfiguration(cfg =>
{
   cfg.CreateMap<Parent, ParentDto>();
   cfg.CreateMap<Child, ChildDto>();
});

IMapper mapper = mapperConfiguration .CreateMapper();

Parent parent = dbContext.Parent.FirstOrDefault();
ParentDto parentDto =  mapper.Map<Parent, ParentDto>(parent);

任何帮助将不胜感激。谢谢。

0 个答案:

没有答案