2 回答

TA贡献1828条经验 获得超3个赞
编写您的GetProjects方法如下:
[HttpGet]
public IEnumerable<Project> GetProjects()
{
return _context.Projects.Include(p => p.Tasks).ToList();
}
然后为避免在类的方法中Self referencing loop添加以下配置:ConfigureServicesStartup
public void ConfigureServices(IServiceCollection services)
{
...
services.AddMvc()
.AddJsonOptions(
options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
...
}

TA贡献1859条经验 获得超6个赞
您可以使用如下所示的包含。您将在项目集合中获得任务集合
// GET: api/Projects
[HttpGet]
public IEnumerable<Project> GetProjects()
{
return _context.Projects.Include(x=>x.Task);
}
- 2 回答
- 0 关注
- 143 浏览
添加回答
举报