views:

43

answers:

1

I am trying to use an MVC application with Entity Framework and Repository pattern In this application, an end user may modify different entities data through multiple http requests during their session. (kind of wizard pages) However they do commit these modifications until a final commit button is clicked These also have the option to leave and this case, their work should be rollbacked.

I am wondering what would happen if two uses are doing the same and one of them clicks the commit button I guess changes made by both users are committed !!!

I guess I need to create a object context by user connection or by session Your comments are very much welcome

+1  A: 

The context should be used only once for fetching the data initially and once for persisting.
(No long-lived 'multi-http-request' contexts).

So what you do is this:

  1. Create context, fetch data, dispose of context.
  2. Manage user changes to the data across multiple requests in whatever way you like (without using the context), e.g. Session, hidden fields, etc.
  3. Create context, persist modified entities, dispose of context.

Regarding step 2 - I recommend using specific objects (ViewModels) rather than EntityObjects in the Views for user interaction.

Yakimych
Yakimych, thank you so much for your input. However, are you suggesting that I should not take advantage from the change tracking provided by EF ObjectContext?If so, why not?
Not in your case. The ObjectContext is a UnitOfWork. It is not thread-safe and meant to be as short-lived as possible. If you use the same ObjectContext across web-requests, or one per multiple users, you can run into a bunch of problems (one of which you mentioned in your question). If you do want to use some of the EF state-tracking features, have a look at Self-Tracking Entities.
Yakimych