I have such entities for that particular issue: Album, Photo. Each Album contains a collection of Photos.
And Dto classes: AlbumWithPhotosDto, which contains a collection of PhotoDto.
In my automapper profile I described the mapping rules for all entities:
CreateMap<Photo, PhotoDto>() .ForMember(dest => dest.AverageRate, opt => opt.MapFrom(src => src.Ratings.Average(r => r.Rate))) .ForMember(dest => dest.RatesCount, opt => opt.MapFrom(src => src.Ratings.Count)) .ReverseMap(); CreateMap<Album, AlbumWithPhotosDto>() .ForMember(dest => dest.PhotosNum, opt => opt.MapFrom(src => src.Photos.Count)) .ReverseMap(); Firstly I used LazyLoadingProxies and when I tried to map an Album to an AlbumWithPhotosDto.
I got exception:
Error mapping types.
Mapping types: Album -> AlbumWithPhotosDto DAL.Entities.Album -> BLL.DataTransferObjects.AlbumWithPhotosDto
Type Map configuration: Album -> AlbumWithPhotosDto DAL.Entities.Album -> BLL.DataTransferObjects.AlbumWithPhotosDto
Destination Member: Photos
Then I changed lazy loading on explicit loading such as:
_context.Albums .Include(a => a.Photos) .Include(a => a.Author) But again I got the same exception. How I can fix this?
Here the additional definitions of classes:
public class Album : IEntity<int> { public int Id { get; set; } /// <summary> /// Album name. /// </summary> public string Name { get; set; } /// <summary> /// Album short description. /// </summary> public string Description { get; set; } /// <summary> /// Whether the other users can view and rate the album photos or not. /// </summary> public bool IsPrivate { get; set; } /// <summary> /// Foreign key, creator's id. /// </summary> public string AuthorId { get; set; } /// <summary> /// User who created the album but not obviously author of all photos. /// </summary> public virtual User Author { get; set; } /// <summary> /// Photos in album. /// </summary> public virtual ICollection<Photo> Photos { get; set; } = new HashSet<Photo>(); } public class Photo : IEntity<int> { public int Id { get; set; } /// <summary> /// Optional photo short description. /// </summary> public string Description { get; set; } /// <summary> /// Path to the photo file on the hard drive. /// </summary> public string FileName { get; set; } /// <summary> /// Date and time of uploading. /// </summary> public DateTime UploadDate { get; set; } /// <summary> /// Photo ratings by users. /// </summary> public virtual ICollection<Rating> Ratings { get; set; } = new HashSet<Rating>(); /// <summary> /// Foreigh key, author's id. /// </summary> public string AuthorId { get; set; } public virtual User Author { get; set; } public int AlbumId { get; set; } public virtual Album Album { get; set; } } public class AlbumWithPhotosDto : IDtoMetadata { public int Id { get; set; } public string Description { get; set; } public string AuthorId { get; set; } public int PhotosNum { get; set; } public ICollection<PhotoDto> Photos { get; set; } } public class PhotoDto : IDtoMetadata { public int Id { get; set; } public string Base64 { get; set; } public string Description { get; set; } public double AverageRate { get; set; } public int RatesCount { get; set; } public DateTime UploadDate { get; set; } public string AuthorId { get; set; } public ICollection<RatingDto> Ratings { get; set; } } https://stackoverflow.com/questions/66835624/cannot-map-inner-navigation-property-using-automapper-ef-core March 28, 2021 at 04:08AM
没有评论:
发表评论