Asp.net mvc 如何通过web api加载复杂数据

Asp.net mvc 如何通过web api加载复杂数据,asp.net-mvc,entity-framework,asp.net-web-api,Asp.net Mvc,Entity Framework,Asp.net Web Api,我有两个对象的模型: 具有多对多关系的项目和单词 public class Project { public int Id { get; set; } public List<ProjectWord> ProjectWords { get; set; } } public class ProjectWord { [Key, ForeignKey("Project"), Column(Order = 0)] public int project

我有两个对象的模型: 具有多对多关系的项目和单词

public class Project
{
public int Id { get; set; }
public List<ProjectWord> ProjectWords { get; set; }
}

public class ProjectWord
    {
        [Key, ForeignKey("Project"), Column(Order = 0)]
        public int project_Id { get; set; }
        [Key, ForeignKey("Word"), Column(Order = 1)]
        public int word_Id { get; set; }
        public Project Project { get; set; }
        public Word Word { get; set; }

    }
当我调用它时,它会返回项目实体,但它的ProjectWords总是空的。(我在数据库中有数据)

试图在我的上下文构造函数中禁用延迟加载

public KwToolsServerContext() : base("name=KwToolsServerContext")
        {
            Configuration.LazyLoadingEnabled = false;
            Configuration.ProxyCreationEnabled = false;
        }
但没有任何帮助


有人能给我指出正确的方向吗?

您是否急切地尝试加载实体?比如:

 db.Projects
   .Include(x => x.ProjectWords)
   .Include(x => x.ProjectWords.Select(y => y.Word))
   .Where(x => x.Id == id)

您是否尝试过急切地加载实体?所以类似于
db.Projects.Include(x=>x.ProjectWords)。其中(x=>x.Id==Id)
这很好,但是在这个例子中,在ProjectWords中,我的Word总是空的。你能告诉我如何在这个请求中加入单词吗?
public KwToolsServerContext() : base("name=KwToolsServerContext")
        {
            Configuration.LazyLoadingEnabled = false;
            Configuration.ProxyCreationEnabled = false;
        }
 db.Projects
   .Include(x => x.ProjectWords)
   .Include(x => x.ProjectWords.Select(y => y.Word))
   .Where(x => x.Id == id)