I have a net core 6 web api endpoint like this (shortened for brevity):
[HttpPut("{id}")]
[Consumes(MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
public async Task<ActionResult<ArbitrationCase>> UpdateCaseAsync(int id, [FromBody] ArbitrationCase arbCase)
{
var orig = await _context.ArbitrationCases
.Include(c => c.Arbitrators)
.Include(c => c.CPTCodes)
.Include(d => d.Notes)
.Include(d => d.OfferHistory)
.FirstOrDefaultAsync(d => d.Id == arbCase.Id);
_context.Entry(orig).CurrentValues.SetValues(arbCase);
// inclusion of the next line will prevent the service from returning a value
// it will return an Ok - 200 response but the response will be empty
// var payor = await _context.Payors.FindAsync(arbCase.PayorId.Value);
await _context.SaveChangesAsync();
return Ok(orig);
}
As mentioned in the code block above, if I uncomment the line that that fetches payor, the service stops returning the "orig" object. Is there some sort of single-use limit on the _context that I am violating? This seems broken to me.
[Edit] Just to be clear, the Payors.FindAsync(...) method does not generate an error. It will either return a value or null if the Payor record does not exist. The return Ok(orig) line is always hit.
[Edit] The controller inherits from a base class that injects _context. This shouldn't be germane to the issue since the _context works when calling SaveChangesAsync but here is the rest of the controller as well as the base class for completeness:
[Route("[controller]")]
[ApiController]
[Authorize]
public class CasesController : MPBaseController
{
private readonly ILogger<CasesController> _logger;
#region Constructor
public CasesController(ILogger<CasesController> logger, ArbitrationDbContext context) : base(context)
{
_logger = logger;
}
#endregion
...
And here is the base class:
public class MPBaseController : ControllerBase
{
protected readonly ArbitrationDbContext _context;
public MPBaseController(ArbitrationDbContext context)
{
_context = context;
}
}
My suspicion is that because the ArbitrationCase class can have a foreign key reference to a record in the Payors table, loading the Payor record with a separate call does something EF or Net Core doesn't like and causes the response stream back to the client to be terminated / truncated somehow. If anyone knows of a way to see some internals of what happens with the response before it goes back to the client - like maybe the serialization of the "orig" object fails because of something the second _context call does - that would be where I would like to look next.