views:

39

answers:

1

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?

+2  A: 

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.

http://stackoverflow.com/questions/3194185/ef-and-repository-pattern-multiple-contexts

Yakimych
I'm actually using the repository pattern over the ObjectContext, but i thought the UnitOfWork implementation only meant that you're saving the changes only at the end, in this case by calling the SaveChanges method. I didn't know the UnitOfWork pattern specifies that the object must be disposed after doing the work.
scripni