c# - 无法使用自动映射器映射内部导航属性。EF 核心
问题描述
对于该特定问题,我有这样的实体:Album
,Photo
。每个都Album
包含一个s的集合。Photo
和 Dto classes: ,其中AlbumWithPhotosDto
包含.PhotoDto
在我的自动映射器配置文件中,我描述了所有实体的映射规则:
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();
首先LazyLoadingProxies
,当我尝试将 an 映射Album
到AlbumWithPhotosDto
.
我有例外:
错误映射类型。
映射类型:相册 -> AlbumWithPhotosDto DAL.Entities.Album -> BLL.DataTransferObjects.AlbumWithPhotosDto
类型映射配置:Album -> AlbumWithPhotosDto DAL.Entities.Album -> BLL.DataTransferObjects.AlbumWithPhotosDto
目的地成员:照片
然后我改变了显式加载的延迟加载,例如:
_context.Albums
.Include(a => a.Photos)
.Include(a => a.Author)
但我又遇到了同样的例外。我该如何解决这个问题?
这里是类的附加定义:
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; }
}
解决方案
您没有Ratings
在查询中包含 。因此,AutoMapper 基本上是在尝试调用.Average()
一个空集合,因此失败了。
将您的查询修改为 -
var albums = _context.Albums
.Include(a => a.Photos).ThenInclude(p => p.Ratings)
.Include(a => a.Author)
.ToList();
推荐阅读
- javascript - 想要使用批处理脚本从 html 文件中提取数据。但我收到一个错误,因为 Object 不支持此属性或方法
- stanford-nlp - 斯坦福 CoreNLP 在存档中缺少无壳模型
- python - Django在保存中获取m2m字段的旧值和新值
- c# - 十进制不转换指数值( jsonConverter 从 JSON 到 C#) - 映射
- php - “无法打开流或文件“laravel.log”:无法打开流:权限被拒绝”
- node.js - node-mailer 使用动态“发件人”电子邮件发送电子邮件
- reactjs - 使用 spring boot 创建-react-app,设置代理服务器
- javascript - nodegit 签出和切换(远程)分支
- cassandra - 如何在cassandra的列表中使用json数据?
- java - Spring Boot 端点未经过身份验证