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 5942293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:13:58+00:00 2026-05-22T16:13:58+00:00

I have a problem about managing concurrency with Entity Framework. I defined a Person

  • 0

I have a problem about managing concurrency with Entity Framework.

I defined a Person class
(I’ll show only the interesting parts)

public class Person
{
   public Guid Id { get; set; }
   public string Name { get; set; }

   public Guid? CategoryId { get; set; }
   public virtual Category Category { get; set; }

   public byte[] TimeStamp { get; set; }
}

and a class for configuration

public class PersonConfiguration : EntityTypeConfiguration<Person>
{
   public PersonConfiguration() : base()
   {
      HasKey(p => p.Id);
      Property(p => p.Id)
         .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

      Property(p => p.TimeStamp)
         .IsConcurrencyToken()
         .HasColumnType("timestamp")
         .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
   }
}

Similarly I defined the Category class and the respective
class for configuration

public class Category
{
   public Guid Id { get; set; }
   public string Title { get; set; }

   public byte[] TimeStamp { get; set; }
}

public class CategoryConfiguration : EntityTypeConfiguration<Category>
{
   public CategoryConfiguration() : base()
   {
      HasKey(c => c.Id);
      Property(c => c.Id)
         .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

      Property(c => c.TimeStamp)
         .IsConcurrencyToken()
         .HasColumnType("timestamp")
         .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
   }
}

In the CommitChanges method I catch the DbUpdateConcurrencyException
exception and with a ‘database wins’ strategy I reload the entity from DB.

public int CommitChanges()
{
  ret = -1;

  try
   {
      ret = _context.SaveChanges();
   }
   catch (DbUpdateConcurrencyException e)
   {
      (e as DbUpdateConcurrencyException).Entries.Single().Reload();
      throw new UowUpdateConcurrencyException();
   }
   catch (Exception e)
   {
         throw new Exception(e.Message);
   }

   return ret;
}

For the purposes of this question we assume that only one entity at a time is saved in DB

There’s no problem if I save a Category. All the tests are ok.

The problem raises when i try to save a Person (previously loaded trough the same context)
and there’s a concurrency violation.
I debug that line

(e as DbUpdateConcurrencyException).Entries.Single().Reload();

Surprise.
The exception is thrown correctly but the entity that come up from the DB
is of type Category instead of Person.

This return Category.

(e as DbUpdateConcurrencyException).Entries.ToList()[0].Entity.GetType()

If I disable the concurrency check for the Category by removing the Timestamp property in
the Category class everything works fine

I think there’s some problem with the navigation property that i defined in the Person class.

What do you think?

Thanks!

UPDATE

It seems that the problem is in the binding that I set between a ComboBox and the Category property.

If I comment it everything works fine

<ComboBox ItemsSource="{Binding Path=CategoryList}"
          DisplayMemberPath="Title"
          SelectedValuePath="Id"
          SelectedItem="{Binding Path=Category, Mode=TwoWay}" />

If I bind the combo to the CategoryId instead to the Category the problem still remains.

<ComboBox ItemsSource="{Binding Path=CategoryList}"
          DisplayMemberPath="Title"
          SelectedValuePath="Id"
          SelectedValue="{Binding Path=CategoryId, Mode=TwoWay}" />
  • 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-22T16:13:59+00:00Added an answer on May 22, 2026 at 4:13 pm

    I found a solution that seems more like a workaround so I don’t accept this as an answer for one or two days in hope that someone can give an explanation.

    The solution consists in load the CategoryList that is ItemsSource for the combo through a different context than that used for Person and for the save operation.
    This prevent to include the Category in the scope of the save.

    Finally, we must set the binding to the CategoryId property and not the Category property.

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

Sidebar

Related Questions

I have problem today about retrieving data from SQLite. Here is my coding public
I have a problem about div position relative alignment. I want the second div
I am using MFC CFile Seek function. I have a problem about Seek out
I have a little problem about using jQuery (I really do not know jQuery
I have hit upon this problem about whether to use bignums in my language
I have got a strange problem about in_array recently which I cannot understand. e.g.
I have got a performance problem about TextField.htmlText +=msg .And I know that TextField.appendText(msg)
i have a problem. i want to get info about my string have other
So I have a problem that I don't really know how to go about.
Following on from a question I posted yesterday about GUIs, I have another problem

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.