Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1087791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:59:07+00:00 2026-05-16T22:59:07+00:00

Evaluating the .NET Entity Framework I try to find the right patterns to handle

  • 0

Evaluating the .NET Entity Framework I try to find the right patterns to handle concurrent updates with optimistic concurrency mode.

In the documentation and many other places I see the following pattern:


Try
  ' Try to save changes, which may cause a conflict. 
  Dim num As Integer = context.SaveChanges()
  Console.WriteLine("No conflicts. " & num.ToString() & " updates saved.")
Catch generatedExceptionName As OptimisticConcurrencyException
  ' Resolve the concurrency conflict by refreshing the 
  ' object context before re-saving changes. 
  context.Refresh(RefreshMode.ClientWins, orders)

  ' Save changes. 
  context.SaveChanges()
  Console.WriteLine("OptimisticConcurrencyException handled and changes saved")
End Try

I see the following problems with this

  • it automatically implements last-in-wins instead of using optimistic mode
  • it is not robust: concurrent changes between .Refresh and .SaveChanges can cause a new OptimisticConcurrencyException

Is this correct, or am I missing something?

In a UI I normally let the user resolve the concurrency conflict:


Try
   _ctx.SaveChanges()
Catch ex As OptimisticConcurrencyException
   MessageBox.Show("Data was modified by another User." & vbCrLf &
   "Click 'Refresh' to show the current values and reapply your changes.",
   "Concurrency Violation", MessageBoxButton.OK)
End Try

In business logic I normally use a retry loop around the whole business transaction (reading and updating):


Const maxRetries = 5, retryDelayMs = 500
For i = 1 To maxRetries
    Try
        Using ctx As New EFConcurrencyTest.ConcurrencyTestEntities
            ctx.Inventories.First.QuantityInStock += 1
            System.Threading.Thread.Sleep(3000) 'Cause conflict
            ctx.SaveChanges()
        End Using
        Exit For
    Catch ex As OptimisticConcurrencyException
        If i = maxRetries Then Throw
        System.Threading.Thread.Sleep(retryDelayMs)
    End Try
Next

With EF I plan to encapsulate the loop:


ExecuteOptimisticSubmitChanges(Of EFConcurrencyTest.ConcurrencyTestEntities)(
    Sub(ctx)
        ctx.Inventories.First.QuantityInStock += 1
        System.Threading.Thread.Sleep(3000) 'Cause conflict
    End Sub)

See:

Functional Optimistic Concurrency in C#

Retryable actions in C#

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T22:59:08+00:00Added an answer on May 16, 2026 at 10:59 pm

    This:

    Catch ex As OptimisticConcurrencyException
      ' Resolve the concurrency conflict by refreshing the 
      ' object context before re-saving changes. 
      context.Refresh(RefreshMode.ClientWins, orders)
    
      ' Save changes. 
      context.SaveChanges()
      Console.WriteLine("OptimisticConcurrencyException handled and changes saved")
    

    …is completely pointless. If the only thing you do when you “handle” the exception is to ignore it and save anyway, you should just turn optimistic concurrency off; you’re writing code to work around an optional feature.

    So, yes, I’d say the documentation is not giving you good advice here.

    Your proposed UI code is a better solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am evaluating VintaSoft .net control and Atalasoft DotTwain Image Capture. And I am
I'm evaluating options to make our search more powerful on our .Net website. I
I'm about to start developing a product (in .NET), and I'm evaluating open source
I'm currently evaluating the MSF for CMMI process template under TFS for use on
I'm evaluating and looking at using CherryPy for a project that's basically a JavaScript
We are currently evaluating different applications that interface with Visual Studio 2008 (C#) and
I'm evaluating Terracotta to help me scale up an application which is currently RAM-bounded.
I'm evaluating Visual Studio productivity addons for my development team, which includes some folks
I'm currently evaluating CodeRush and one thing that I liked most when reading the
I've recently begun evaluating a few project management projects for the company I work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.