-
Hi. Im interested correct use DbContext if long time request (e.g upload file). Demo example: [ApiController]
public class TestController : ControllerBase
{
private ApplicationDbContext _context;
public TestController (ApplicationDbContext context)
{
_context = context;
}
[HttpPost]
public async Task<IActionResult> LongPost()
{
var items = await _context.Items.Where(s=>s.UserId = 10).ToArrayAsync();
if(items.Length == 0) return NotFound();
if(items[0].Val == 100) return Ok(items);
// Long request emulation
await Task.Delay(100000);
items[0].Val = 100;
await _context.SaveChangesAsync();
return Ok(items);
}
} As far as I know, after a long operation, the context is not recommended to be used. And the example above is not correct use of DbContext. If so, tell me how to use DbContext correctly in such a situation. |
Beta Was this translation helpful? Give feedback.
Answered by
WeihanLi
Nov 19, 2020
Replies: 1 comment 1 reply
-
Create the |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
sneltyn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Create the
dbContext
only when you need, reduce the scope of thedbContext
and dispose it in time if possible