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

  • Home
  • SEARCH
  • 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 9015877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:51:35+00:00 2026-06-16T03:51:35+00:00

The use EF DbContext. My entity object has rowversion column (SQL Compact edition ver

  • 0

The use EF DbContext. My entity object has rowversion column (SQL Compact edition ver 4), which is used for concurrency check (ConcurrencyMode = Fixed, StoreGeneratedPattern=Computed).

To force concurrency exception, from UI I read the same table record in 2 different forms, edited each of them, and saved one after another. The following code does actual save operation.

When save button on the 2nd form clicked, concurrency error occurs as expected. However,
the exception still persists at the second attempt, after copying original values from the database. Only third attempt succeeds without any error. Can someone explain me what might cause this problem?

try
{
  _ctx.SaveChanges(); //first attempt
}
catch (Exception ex)
{
  if (ex is DbUpdateConcurrencyException)
  {
    var exc = ex as DbUpdateConcurrencyException;
    foreach (var entry in exc.Entries)
      entry.OriginalValues.SetValues(entry.GetDatabaseValues());
    try
    {
      _ctx.SaveChanges(); //second attempt
    }
    catch (Exception ex2)
    {
      if (ex2 is DbUpdateConcurrencyException)
      {
        var exc2 = ex2 as DbUpdateConcurrencyException;
        foreach (var entry in exc2.Entries)
          entry.OriginalValues.SetValues(entry.GetDatabaseValues());
        try
        {
          _ctx.SaveChanges(); //third attempt
        }
        catch (Exception ex3)
        {
          System.Windows.MessageBox.Show(ex3.Message);
        }
      }
    }
  }
}

EDIT: I found that it occurs when I make both updates via UI. If in the above code, before first attempt, I do something the following:

var _ctx2 = new MyDbContext();
var myEntity = _ctx2.MyEntities.Where(ent => ent.Id == 2).Single();
myEntity.Name = "My new name";
_ctx2.SaveChanges();
_ctx2.Dispose();

then the code works as expected, given that another instance of myEntity was updated via UI; i.e second attempt will save myEntity.
And, the problem lies at the following line:

foreach (var entry in exc.Entries)
  entry.OriginalValues.SetValues(entry.GetDatabaseValues());

because, when updated via UI, exc.Entries returns not the entity at which concurrency error happened, but its navigation property entity.

In this case, MyEntity is a tree-like self- referencing entity, which has two navigation properties: ParentEntity, and Children.

So, after the first save attempt, what I have in exc.Entries is ParentEntity (in a unchanged state), and only after the second save attempt, exc.Entries returns actual entity at which concurrency error thrown.

  • 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-06-16T03:51:36+00:00Added an answer on June 16, 2026 at 3:51 am

    Ok, it seems an EF bug. See the following:

    http://support.microsoft.com/kb/2390624#appliesto
    http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/ce60bf40-cd05-42f6-ab8f-26b048ec83d7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several Entity Framework Code First DbContext objects that use a custom Initializer.
The goal: I'm trying to use the new Entity Framework 4.1 DbContext API (using
I have .NET MVC 3 project where I use sql membership provider and entity
I am trying to use Linq to create an object which itself contains 2
Can I use Entity Framework DbContext, Code First approach, Data Annotation validation and customize
I'm trying to use the Entity Framework 4.1 RC with a SQL Server 2005
I have a existing DB and I use the approach DB First with Entity
I'm trying to use LINQPad 4 (the latest beta) with Entity Framework POCO classes.
I use entity framework and have a set of users: public class DbModel :
I'm using Entity Framework 5.0 (for .NET 4) and making use of the TimestampAttribute

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.