Refresh ObjectContext or recreate it to reflect changes made to the database?
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 wo开发者_如何学JAVAuld 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?
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
精彩评论