这里是存储库,在其中创建了一种方法,该方法中需要基于中间表的数据。
public Task<IEnumerable<DepartmentSchool>> GetAllDepartmentBySchoolIdAsync(int schoolId)
{
var school = _GpsContext.School.Where(e => e.ID == schoolId).FirstOrDefaultAsync();
if (school == null)
return NotFound();
var departments = _GpsContext.DepartmentSchool
.Where(e => e.SchoolsId == schoolId).Select(e => e.DepartmentID);
return Ok(departments);
}
然后从我的控制器类中调用该方法以基于该方法获取值。
[Route("api/[controller]")]
[ApiController]
public class DepartmentSchoolController : ControllerBase
{
private readonly IDepartmentSchoolRepository _departmentSchoolRepository;
public DepartmentSchoolController(DepartmentSchoolRepository departmentSchoolRepository)
{
_departmentSchoolRepository = departmentSchoolRepository;
}
/// <summary>
/// Calling depatment on the basis of school id.
/// </summary>
/// <param name="schoolId"></param>
/// <returns></returns>
[HttpGet("school/{schoolId}/departments")]
public async Task<IEnumerable<DepartmentSchoolRepository>> GetDepartmentsFromSchool(int schoolId)
{
return await _departmentSchoolRepository.GetAllDepartmentBySchoolIdAsync();
}
可以,请告诉我问题出在哪里。上面的代码不起作用,并且在NotFound()
和Ok()
中显示出“当前上下文中不存在”的问题,我该怎么办?
这里是相关实体:
public class DepartmentSchool
{
public int Id { get; set; }
public int DepartmentID { get; set; }
[ForeignKey("DepartmentID")]
public virtual Department Department { get; set; }
public int SchoolsId { get; set; }
[ForeignKey("SchoolsId")]
public virtual Schools Schools { get; set; }
}
public partial class Schools
{
public int ID { get; set; }
public DateTime UpdatedAt { get; set; }
}
public partial class Department
{
public int Id { get; set; }
public string Name { get; set; }
public int SchoolSystemsID { get; set; }
}
答案 0 :(得分:0)
您的代码有很多问题,包括无法编译。
在您的控制器中:
IActionResult
类型用作per the docs
[HttpGet("school/{schoolId}/departments")]
public async Task<IActionResult> GetDepartmentsFromSchool(int schoolId)
{
var departments = await _departmentSchoolRepository
.GetAllDepartmentBySchoolIdAsync(schoolId);
if (departments.Any())
{
return Ok(departments)
}
return NotFound();
}
在您的存储库中:
Task
而不是您期望的EF实体类型。.ToListAsync()
)async
Ok
/ NotFound
,这应该保留给您的控制器层。ds
中使用了DepartmentSchool
,在下面的s
中使用了School
。
// ** Return type is the projected result
public async Task<IEnumerable<Department>> GetAllDepartmentBySchoolIdAsync(int schoolId) // ** async
{
var school = await _GpsContext.School // ** await
.Where(s => s.ID == schoolId)
.FirstOrDefaultAsync();
if (school == null)
return Enumerable.Empty<Department>(); // Avoid returning nulls
var departments = await _GpsContext.DepartmentSchool // ** await
.Where(ds => ds.SchoolsId == schoolId)
.SelectMany(ds => ds.Department) // ** flatten and project the navigation property
.ToListAsync(); // ** materialize
return departments; // ** Don't use Http stuff like OK in Repo layer
}
还请注意,您的第一个查询实际上是多余的。如果您在数据库中强制实施了参照完整性,则不可能有DepartmentSchool
个没有链接的School
的联结行,因此您应该只寻找DepartmentSchool
行。 / p>
在上面,我假设您已经启用了延迟加载。如果您不这样做,则需要将DepartmentSchool -> Department
导航加载为per here。