Note that there are some explanatory texts on larger screens.

plurals
  1. POConcurrency with Entity Framework & SQL (ADO.NET)
    primarykey
    data
    text
    <p>I have an issue with some concurrency. I read into the Optimistic Concurrency Handling in <a href="http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx" rel="nofollow">http://blogs.msdn.com/b/alexj/archive/2009/05/20/tip-19-how-to-use-optimistic-concurrency-in-the-entity-framework.aspx</a></p> <p>I have a piece of code to test this in practice in my WCF service:</p> <pre><code> public GameResult PurchaseGameItem(int itemId) { using (var entities = new GameEntities()) { var p1 = entities.Items.Where(p =&gt; p.ID == itemId).FirstOrDefault(); p1.Coins = 1; using (var e2 = new GameEntities()) { var p2 = e2.Items.Where(p =&gt; p.ID == itemId).FirstOrDefault(); p2.Coins = 2; e2.SaveChanges(); } entities.SaveChanges(); } return GameResult.Success; } </code></pre> <p>Furthermore, I have set the "Coins" Concurrency Mode in the model to "Fixed". However, this goes through and the value saved in the database is "1". I have tried with a few sets of values so it's not that it is "correct by accident". Why isn't an OptimisticConcurrencyException being raised here?</p> <p><strong>[update #1]</strong> By trying out the first response below and combining them, I managed to get a DbUpdateConcurrencyException out <em>sometimes</em>. It's still not the OptimisticConcurrencyException I am expecting but it is something. Basically I have no clue why it is not happening all the time.</p> <pre><code> var p1 = entities.Items.Where(p =&gt; p.ID == itemId).FirstOrDefault(); var p3 = entities.Items.Where(p =&gt; p.ID == itemId).FirstOrDefault(); p1.CoinValue = 14; p3.CoinValue = 115; using (var e2 = new GameEntities()) { var p2 = e2.Items.Where(p =&gt; p.ID == itemId).FirstOrDefault(); p2.CoinValue = 72; e2.SaveChanges(); } entities.SaveChanges(); // This throws DbUpdateConcurrencyException, perhaps 50% of the time. </code></pre> <p>Looks like there is a concurrency issue here!! :D</p> <p><strong>[update #2]</strong> Alright, I figured a part of it now and it was mostly stupid user error. DbUpdateConcurrencyException is thrown only once since second time, the second clause will update it to the value it is already in, so that part is logical. If two entities have been fetched from the same context, the latter change will simply be taken into account so in a "one database connection" case as suggested by the first response, no concurrency exception is raised. This is logical since it is up to the programmer to not access the object through multiple handles.</p> <p>What I do not get though is the difference between DbUpdateConcurrencyException and OptimisticConcurrencyException. I have not being able to raise the latter.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload