user385411
user385411

Reputation: 1465

Entity Framework ObjectContext: Concurrency

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

Upvotes: 2

Views: 1688

Answers (1)

Yakimych
Yakimych

Reputation: 17752

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.

Upvotes: 1

Related Questions