entity-framework-core - LINQ 查询在 FirstOrDefault 方法上引发异常
问题描述
我正在使用 EF 核心,并且两个实体之间存在多对多关系
IotaProject <--> 用户
这是与问题相关的实体和 dto
public class IotaProject
{
[Key]
public int Id { get; set; }
[Required]
public string ProjectName { get; set; }
[Required]
public DateTime Create { get; set; }
public ICollection<ProjectOwnerJoint> Owners { get; set; } = new List<ProjectOwnerJoint>();
}
public class ProjectOwnerJoint
{
public int IotaProjectId { get; set; }
public IotaProject IotaProject { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
public class User
{
[Key]
public int Id { get; set; }
[Required]
public string FullName { get; set; }
[Required]
public string ShortName { get; set; }
[Required]
public string Email { get; set; }
public ICollection<ProjectOwnerJoint> OwnedProjects { get; set; } = new List<ProjectOwnerJoint>();
}
public class ApplicationDbContext : DbContext
{
public DbSet<IotaProject> IotaProjects { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<ProjectOwnerJoint> ProjectOwnerJoint { get; set; }
}
public class IotaProjectDisplayDto
{
public int Id { get; set; }
public string ProjectName { get; set; }
public DateTime Create { get; set; }
public UserMinDto Owner { get; set; }
public int Count { get; set; }
public IEnumerable<UserMinDto> Reviewers { get; set; }
}
public class UserMinDto
{
public int Id { get; set; }
public string FullName { get; set; }
public string ShortName { get; set; }
}
以下是LINQ的问题,LINQ的目的是将IotaProject转换为IotaProjectDisplayDto,关键是IotaProject的Owners属性是ICollection,而IotaProjectDisplayDto中的Owner属性只是一个单元素UserMinDto,所以我只需要获取IotaProject的第一个元素所有者,那就是 FirstOrDefault() 来了。
IEnumerable<IotaProjectDisplayDto> results = _db.IotaProjects.Select(x => new IotaProjectDisplayDto
{
Id = x.Id,
ProjectName = x.ProjectName,
Create = x.Create,
Owner = x.Owners.Select(y => y.User).Select(z => new UserMinDto { Id = z.Id, FullName = z.FullName, ShortName = z.ShortName }).FirstOrDefault()
});
return results;
它抛出运行时异常
Expression of type 'System.Collections.Generic.List`1[ToolHub.Shared.iota.UserMinDto]' cannot be used for parameter
of type 'System.Linq.IQueryable`1[ToolHub.Shared.iota.UserMinDto]'
of method 'ToolHub.Shared.iota.UserMinDto FirstOrDefault[UserMinDto](System.Linq.IQueryable`1[ToolHub.Shared.iota.UserMinDto])' (Parameter 'arg0')
我猜这可能与延迟执行有关,但是在阅读了一些帖子后,我仍然无法解决。
任何提示将不胜感激。
现在,我可以完成这项工作的唯一方法是将 IotaProjectDisplayDto 中的 Owner 属性类型更改为 IEnumrable,这将不再需要 FirstOrDefault() 来立即执行。后来,我手动让客户端中的第一个元素显示。
解决方案
此问题发生在 Microsoft.EntityFrameworkCore.SqlServer 3.0.0-preview7.19362.6
正如 Ivan 在评论中建议的那样,我最终降级到 EF core stable 2.2.6,一切正常。
推荐阅读
- docker - Docker-机器创建失败
- jquery - HTML表单中的浏览器自动完成属性不起作用
- ember.js - ember-cordova 无法使用 x-file-input 多次上传照片
- jenkins - 如何从触发它的工作中触发 Jenkins 工作?
- groovy - 覆盖groovy中的增量运算符
- python - 我们如何将fft应用于(105,36)的这个文件并读取行并将其存储到(105,18)的新数组中并绘制图形
- delphi - 如何在 Delphi 中强制分层源代码结构
- android - 当像 messenger 这样的覆盖应用程序在前台时,Android 应用程序进程被破坏
- javascript - 如何将鼠标悬停用于隐藏在不可见元素后面的元素?
- drag - Mapbox GL JS:在多点地图中拖动 1 个点