Refresh ObjectContext or recreate it to reflect changes made to the database?

萝らか妹 提交于 2020-01-16 19:06:25

问题


I have a web service that exposes operations to and from a database. I'm using Entity Framework 4 for the DAL.

I'm wondering what would be the best way to handle operations to and from the database. Would it be best not to keep alive an instance of the ObjectContext and instead create one for each operation done to the database? This seems to be a very bad way to handle this, but i can't think of another way to keep up with the changes made to the database.

If i keep the same context alive, how will it know when data has been modified? I understand that when querying the database, the ObjectContext just checks if records have been added or deleted, by checking the Primary Key fields (am i correct? ). In that case, i have to explicitly tell it to refresh a particular object, which is not very practical.

I'm thinking of a solution where a timestamp column is used, and the objectcontext checks that column for changes. If that column is modified, it refreshes the data. Is such a solution built inside Entity Framework 4? Or does anyone have any other suggestions on how i can always get the latest data from the database without recreating the ObjectContext for each request?


回答1:


The ObjectContext is meant to be used as a Unit of work, so you create one for every business transaction and dispose of it afterwards.

EF - and repository pattern - multiple contexts



来源:https://stackoverflow.com/questions/3298057/refresh-objectcontext-or-recreate-it-to-reflect-changes-made-to-the-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!