c# - EF - 嵌套包含抛出错误

标签 c# entity-framework linq asp.net-core entity-framework-core

我有一个跟踪学校项目的应用程序。项目有主题,而这些主题有任务。尽管一个项目和一个团队已经确定了领导者。学生应该轮流担任不同任务和科目的领导者(科目是我们所涵盖的不同书籍的部分)。我注意到在我的主题和任务的对象中,这些对象的领导者没有被加载。所以我试图在我的 Entity Framework 查询中添加一个我在网上看到的嵌套包含,以便它们被加载。但是,当我运行我的查询时,它给了我这个错误。 任何人都可以看到我做错了什么并且知道如何解决这个问题吗?

            string name = data.value;
            var project = await context.Projects
            .Include(p => p.Subjects)
                .ThenInclude(s => s.Tasks)
            .Include(p => p.Subjects.Select(s => s.Leader))
            .Include(p => p.Students)
                //.ThenInclude(h => h)
            .Include(p => p.Leader)
            .Include(p => p.Team)
                .ThenInclude(t => t.Leader)
            .Include(p => p.Type)
            .AsNoTracking()
            .FirstOrDefaultAsync(p => p.Name == name);

项目.cs

public class Project
    {
        [Key]
        public int ID { get; set; }

        public int TypeID { get; set; }
        public int? TeamID { get; set; }
        public int? LeaderID { get; set; }

        public string Name { get; set; }
        public string Number { get; set; }
        public string Details { get; set; }
        public bool IsActive { get; set; }


        [ForeignKey("TypeID")]
        public virtual ProjectType Type { get; set; }
        [ForeignKey("TeamID")]
        public virtual Team Team { get; set; }
        [ForeignKey("LeaderID")]
        public virtual User Leader { get; set; }

        public virtual ICollection<Subject> Subjects{ get; set; }
        public virtual ICollection<User> Students { get; set; }


 public Project()
        {
            Subjects= new List<Subject>();
            Students = new List<User>();
        }
    }

主题.cs

public class Subject
{
    [Key]
    public int ID { get; set; }
    // 
    public int? ProjectID { get; set; }
    public int TypeID { get; set; }
    public int? LeaderID{ get; set; }

    public string Name { get; set; }
    public string Details { get; set; }
    public Decimal Estimate { get; set; }


    [ForeignKey("ProjectID")]
    public virtual Project Project { get; set; }
    [ForeignKey("TypeID")]
    public virtual SubjectType Type { get; set; }
    [ForeignKey("LeaderID")]
    public virtual User Leader { get; set; }

    public virtual ICollection<Task> Tasks { get; set; }

    public Subject()
    {
        Tasks = new List<Task>();
    }

任务

  public class Task
    {
        [Key]
        public int ID { get; set; }
        public int? SubjectID { get; set; }
        public int? TypeID { get; set; }
        public int? LeaderID{ get; set; }

        public string Name { get; set; }
        public string Details { get; set; }
        public Decimal Estimate { get; set; }

        [ForeignKey("SubjectID")]
        public virtual Subject Subject { get; set; }
        [ForeignKey("TypeID")]
        public virtual TaskType Type { get; set; }
        [ForeignKey("LeaderID")]
        public virtual User Leader { get; set; }
        public Task() { }
    }

错误

Message = "The property expression 'p => {from Subjects s in [p].Subjects select [s].Leader}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwl...

最佳答案

错误信息已经告诉你了。

您有 .Include(p => p.Subjects.Select(s => s.Leader)),这就是您在 EF6 中嵌套包含的方式。在 EF Core 中,您必须使用 .ThenInclude!

所以只需将 .Include(p => p.Subjects.Select(s => s.Leader)) 更改为 .Include(p => p.Subjects).ThenInclude( s => s.Leader))

关于c# - EF - 嵌套包含抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43527171/

相关文章:

c# - 如何避免 TypeInitializer 异常?

c# - 如何从图像生成突出颜色的调色板?

C# 继承和方法签名

c# - Entity Framework LINQ查询返回相同的值

c# - 为什么 2 个 LINQ 查询得到不同的评估

c# - 将列表<int> 转换为连接的整数字符串?

c# - 避免数据表列上的 LINQ 表达式中出现 NullReferenceException

c# - 如何对多线程场景中只执行一次的代码进行单元测试?

asp.net - EF 使用代码优先和一对多关系创建重复的外键

c# - Count 或 Skip(1).Any() 我想知道是否有超过 1 条记录的地方 - Entity Framework