Entity Framework ObjectContext re-usage

后端 未结 6 576
我寻月下人不归
我寻月下人不归 2020-12-24 15:45

I\'m learning EF now and have a question regarding the ObjectContext:

Should I create instance of ObjectContext for every query (function) when I access the database

6条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-24 16:13

    Like Luke says this question has been asked numerous times on SO.

    For a web application, per request cycle seems to work best. Singleton is definitely a bad idea.

    Per request works well because one web page has a User, maybe some Projects belonging to that user, maybe some Messages for that user. You want the same ObjectContext so you can go User.Messages to get them, maybe mark some messages as read, maybe add a Project and then either commit or abandon the whole object graph at the completion of the page cycle.

提交回复
热议问题