1
的Visual Studio生成对于我这个伟大航路,在那里我可以加载一个实体:实体框架贪婪加载网页API路线
// GET: api/Jobs/5
[ResponseType(typeof(Job))]
public async Task<IHttpActionResult> GetJob(int id)
{
Job job = await db.Jobs.FindAsync(id);
if (job == null)
{
return NotFound();
}
return Ok(job);
}
这里的Job
模型,这是基于:
public class Job
{
public Job()
{
this.Regions = new List<Region>();
this.Files = new List<JobFile>();
}
public int ID { get; set; }
public string Name { get; set; }
public List<Region> Regions { get; set; }
public JobTypes JobType { get; set; }
public int UserIDCreatedBy { get; set; }
public int? UserIDAssignedTo { get; set; }
public List<JobFile> Files { get; set; }
public bool IsLocked { get; set; } // Lock for modification access
}
这里的在JobFile
类,它Job
■找的列表:
public class JobFile
{
public int ID { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public int Job_ID { get; set; }
}
和Pdf
的JobFile
一个子类:
public class Pdf : JobFile
{
public Pdf()
{
this.PdfPages = new List<PdfPage>();
}
public int Index { get; set; }
public List<PdfPage> PdfPages { get; set; }
}
现在,当我打的路线,我想急切地加载所有的Pdf
■对于Job
。我正在努力如何使用Linq编写该查询,特别是在继承这里的子类的情况下。我需要在路由替换该行:
Job job = await db.Jobs.FindAsync(id);
我有一个开始吧,我认为:
// Eager load all the properties
IQueryable<Pdf> jobPdfs = (IQueryable<Pdf>)db.JobFiles.Where(jobFile => jobFile.Job_ID == id && jobFile is Pdf);
这应该让我所有的Pdf
的I认为。但我也想加载PdfPages
的Pdf
s。我如何修改此声明以包含该声明?要做的事情似乎是使用.Include(pdf => pdf.PdfPages)
,
我认为我们有这个[解决](HTTP:// codereview.stackexchange.com/a/119699/7251)? –