Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a DbContext with postbacks?

Perhaps a simple question but I can't find the answer. I have a web form in which I declare my dbContext. However, when the page does postback, it overwrites the dbContext and I lose my object tracking. How do I keep the dbContext secure from changes until I'm ready to dispose of it?

like image 598
proseidon Avatar asked Mar 12 '26 00:03

proseidon


1 Answers

The suggested pattern for using DbContext is to instantiate a new DbContext per HTTP request, and use that instance for the duration of that particular request.

You should not be attempting to keep an instance of DbContext across HTTP requests.

like image 138
Eric J. Avatar answered Mar 14 '26 14:03

Eric J.