我想使用AutoMapper将EfCore多对多实体映射到一对多对象。
我的课程:
Public Class App
{
public Guid Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Summary { get; set; }
public ICollection<AppCollection> AppCollections { get; set; }
}
public class Collection
{
public Guid Id { get; set; }
public string Name { get; set; }
public ICollection<App> Apps { get; set; }
public ICollection<AppCollection> AppCollections { get; set; }
}
public class AppCollection
{
public Guid Id { get; set; }
public Guid AppId { get; set; }
public App App { get; set; }
public Guid CollectionId { get; set; }
public Collection Collection { get; set; }
}
Public Class AppResponse
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Summary { get; set; }
public ICollection<CollectionResponse> AppCollections { get; set; }
}
public class CollectionResponse
{
public Guid Id { get; set; }
public string Name { get; set; }
}
App类是我的数据库实体对象,而AppResponse是我的api响应类。我也不想在mapper中创建新对象。 我已经尝试了以下映射器配置,但无法正常工作。它会忽略Collection对象。
CreateMap<App,AppResponse>()
.ForMember(x => x.IconUrl,c => c.MapFrom(s => s.IconLink))
.ForMember(m => m.AppCollections,c => c.MapFrom(s => s.AppCollections.Select(x => x.Collection).ToList()));