C# 无法获取efcore中的嵌套表

C# 无法获取efcore中的嵌套表,c#,.net,ef-core-2.0,C#,.net,Ef Core 2.0,嗨,我在efcore工作。我的表格结构如下 public class User { public User() { this.Projects = new HashSet<Project>(); } [Key] public int Id { get; set; } public string name { get; set; } public string emailId { get; set; } p

嗨,我在efcore工作。我的表格结构如下

public class User
{
    public User()
    {
        this.Projects = new HashSet<Project>();
    }
    [Key]
    public int Id { get; set; }
    public string name { get; set; }
    public string emailId { get; set; }
    public virtual ICollection<Project> Projects { get; set;}
}

public class Project
{   
    public Project()
    {
        this.TimeSheetData = new HashSet<TimeSheetData>();
    }
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public int userId { get; set; }
    [ForeignKey("userId")]
    public virtual User User {get; set; }
    public virtual ICollection<TimeSheetData> TimeSheetData { get; set;}
}

public class TimeSheetData
{
    [Key]
    public int id { get; set; }
    public int project_id { get; set; }
    [ForeignKey("project_id")]
    public virtual Project Project {get; set; }
    public string hours_logged { get; set; }
}
这给了我下面的错误

The Include property lambda expression 'u => {from Project p in u.Projects select [p].TimeSheetData}' is invalid. The expression should represent a property access: 't => t.MyProperty'. To target navigations declared on derived types, specify an explicitly typed lambda parameter of the target type, E.g. '(Derived d) => d.MyProperty'
我在文件中使用了上述名称空间

using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using TimeSheet.DataLayer;
using Microsoft.EntityFrameworkCore;

有人能帮我解决这个问题吗。任何帮助都将不胜感激。谢谢

我想你的问题是你正在使用.Include()中的.Select()。Include将使用您在类中定义的导航属性将数据连接在一起

我认为您应该使用include获取数据,然后使用.Select()选择所需内容。大概是

public User GetTimeSheet(int userid)
    {
         return _context.Users.Where(u => u.Id ==userid).Include(u => u.Projects).ThenInclude(t=> t.TimeSheetData).FirstOrDefault().Select(d=>d.TheDataYouWant);
    }
将数据保存到一个变量中,然后返回所需的片段,这样可能会比较容易混淆。将这么多东西链接在一起,很容易在自己的代码中迷失方向

    public User GetTimeSheet(int userid)
        {
             var mydata =  _context.Users.Where(u => u.Id ==userid).Include(u => u.Projects).ThenInclude(t=> t.TimeSheetData).FirstOrDefault().Select(d=>d.TheDataYouWant);
             return mydata.myUser;
        }

在您的数据库上下文中,
Project
表是否定义了与
User
的fk关系?是的,我已经添加了代码。谢谢您的回答。FirstOrDefault()。在代码var data=\u context.Users.Include(u=>u.Projects)下面选择giving me compile error。然后选择Include(p=>p.TimeSheetData)。其中(a=>a.Id==userid)。ToList();提供所有数据,但返回时用户对象将只有用户和项目数据。根据我的经验,使用thenClude()返回嵌套项。您可能需要查看项目数据,看看它是否将timesheetdata类嵌套为属性
    public User GetTimeSheet(int userid)
        {
             var mydata =  _context.Users.Where(u => u.Id ==userid).Include(u => u.Projects).ThenInclude(t=> t.TimeSheetData).FirstOrDefault().Select(d=>d.TheDataYouWant);
             return mydata.myUser;
        }