Manage the lifetime of dbContext

前端 未结 5 1541
太阳男子
太阳男子 2020-12-31 09:56

I would like to tie the lifetime of a dbContext to the lifetime of a session, to - for example - be able to commit or abandon changes on a group of mutations on the dbcontex

5条回答
  •  半阙折子戏
    2020-12-31 10:34

    In theory you can store a context into the Session dictionary that you can access in each controller. However you might have some threading problem, because at time you store it you are in a different thread than when you retrieve it. If context doesnt use threadstatic varaibles this might work (but I am not sure of this), otherwise not. In any case this is bad design ...none in the web do this...why do you want to store context? You can re-create it at a cheap price in ths subsequent http request. If you need to track changes of properties there are other ways more adequate to the web.

提交回复
热议问题