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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:33:51+00:00 2026-06-06T14:33:51+00:00

I am trying to learn how Entity Framework works. I understand that EF SaveChanges

  • 0

I am trying to learn how Entity Framework works. I understand that EF SaveChanges is basically a wrapper around a transactional update. I also understand that you can wrap two context’s in a TransactionScope if necessary.

I am using the Code First approach.

What I dont understand is how I would do an update like

UPDATE Inventory SET Available = Available - 1 WHERE Available > 0

In other words – How do I ensure that there is at least X Available inventory before doing the update?

I suppose I could write code that looks at the inventory of the product and verifies that there is enough inventory to complete a purchase:

if (Product.Inventory - quantityToPurchase < 0) throw new Exception(..)

But what about in scenario where two customers attempt to buy at the same time and the object fetched from the database for each customer claims to have 2 items in inventory? My logic above would not catch that.

How do I ensure that the SaveChanges() method only commits the changes to the object IF and ONLY IF (Available - quantity) is greater than 0?

  • 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-06T14:33:54+00:00Added an answer on June 6, 2026 at 2:33 pm

    Entity Framework by default uses Optimistic Concurrency. What I described in my original post is called Pessimistic Concurrency in EF. There are two attributes that can be used to achieve this.

    First is Timestamp which will create a rowversion column in the table. EF will detect this automatically and will always add a where clause to the update comparing the value of the property from the entity to the value of the column in the database. The update statement succeeds only if the two match. The rowversion column is incremented by the server automatically. This ensures that you are always updating the same data that you have in memory (in the POCO). If someone else changes that row after you fetched it and before you updated it, the update will fail.

    Second is ConcurrencyCheck which works simliar to Timestamp. It will cause the value of the POCO property to be included in the where clause. The big difference is that it will not update this column like the rowversion will.

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

Sidebar

Related Questions

I'm brand new to the Entity Framework and trying to learn all it can
I'm trying to learn ASP.NET MVC + Entity Framework by developing a small project.
i'm trying to learn Entity Framework and I need some help to build my
I'm trying to learn to work with Hibernate but probably i don't understand @ManyToOne
Trying to learn Regex in Python to find words that have consecutive vowel-consonant or
Let's say I have an Entity in my Entity Framework Model that is named
I've heard it said that the Entity Framework is overkill or that it's difficult
I'm trying to learn the zend framework by reading through a book. So far
I'm trying to learn Hibernate and I wrote the simplest Person Entity and I
I'm trying to do an Entity Framework walkthrough so I: downloaded SQL script here:

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.