Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The previous values need to be in your object.</p> <p>Let's say you have a property <code>ConcurrencyToken</code>:</p> <pre><code>public class MyObject { public Guid Id { get; set; } // stuff public byte[] ConcurrencyToken { get; set; } } </code></pre> <p>Now you can set <code>ConcurrencyMode.Fixed</code> on that property. You also need to configure your DB to automatically update it.</p> <p>When you query the DB, it will have some value:</p> <pre><code>var mo = Context.MyObjects.First(); Assert.IsNotNull(mo.ConcurrencyToken); </code></pre> <p>Now you can detach or serialize the object, but you need to include <code>ConcurrencyToken</code>. So if you're putting the object data on a web form, you'll need to serialize <code>ConcurrencyToken</code> to a string and put it in a hidden input.</p> <p>When you <code>ApplyChanges</code>, you need to include the <code>ConcurrencyToken</code>:</p> <pre><code>Assert.IsNotNull(myObject.ConcurrencyToken); using (var context = new MyContext()) { context.MyObjects.ApplyChanges(myObject); context.SaveChanges(); } </code></pre> <p>Having <code>ConcurrencyMode.Fixed</code> changes the <code>UPDATE</code> SQL. Normally it looks like:</p> <pre><code>UPDATE [dbo].[MyObject] SET --stuff WHERE [Id] = @0 </code></pre> <p>With <code>ConcurrencyMode.Fixed</code> it looks like:</p> <pre><code>UPDATE [dbo].[MyObject] SET --stuff WHERE [Id] = @0 AND [ConcurrencyToken] = @1 </code></pre> <p>...so if someone has updated the row between the time you read the original concurrency token and the time you saved, the <code>UPDATE</code> will affect 0 rows instead of 1. The EF throws a concurrency error in this case.</p> <p><strong>Therefore</strong>, if any of this isn't working for you, the first step is to use SQL Profiler to look at the generated <code>UPDATE</code>.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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