Here is Repository, in which I created a methods in which I needed the data on the basis of intermediate table.
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);
}
And the method is called from my controller class to get value on the basis of the methods.
[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();
}
Can, please tell me where is the problem. Above code is not working and also showing problem in NotFound()
and Ok()
'does not exist in the current context', what I have need to do?
Here is related entity:
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; }
}