I changed my Apartments Model Class by adding a BuyerID which is a foreign key to another Buyer Class like this:
public class Apartment
{
[Key]
public int ID { get; set; }
public string Title { get; set; }
public int NbofRooms { get; set; }
public int Price { get; set; }
public string Address { get; set; }
public int BuyerId { get; set; }
}
Also I have my Buyers Model Class as the following:
public class Buyer
{
[Key]
public int ID { get; set; }
public string FullName { get; set; }
public int Credit { get; set; }
public ICollection<Apartment> apartments { get; set; }
}
So it also contains a collection of Apartments. and because of this maybe my Get method isn't working anymore and is returning the following error: GET http://localhost:54632/api/Apartments net::ERR_CONNECTION_RESET 200 (OK)
The only GET Method not working is this one:
// GET: api/Apartments
[HttpGet]
public IEnumerable<Apartment> GetApartments()
{
return _context.Apartments;
}
Otherwise the others such as this:
// GET: api/Apartments/5
[HttpGet("{id}")]
public async Task<IActionResult> GetApartment([FromRoute] int id)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var apartment = await _context.Apartments.SingleOrDefaultAsync(m => m.ID == id);
if (apartment == null)
{
return NotFound();
}
return Ok(apartment);
}
is working fine.Also if I try the link on chrome it returns the apartments but if I try it on Postman or Angular App it returns the error. What could be the cause of this error? Thank you.