title: EF Core Concurrency not being enforced
body: <p>I've followed the documentation here, and I'm using SQL Server 2016.(<a href="https://ef.readthedocs.io/en/latest/modeling/concurrency.html" rel="nofollow">https://ef.readthedocs.io/en/latest/modeling/concurrency.html</a>)</p>
  
  <p>I've added a timestamp column to my table named VersionCol, and then when I run Scaffold-DbContext it places the following property on my table entity in my DbContext.</p>
  
  <pre><code>entity.Property(e =&gt; e.VersionCol)
      .IsRequired()
      .HasColumnType("timestamp")
      .ValueGeneratedOnAddOrUpdate()
  </code></pre>
  
  <p>It's missing <code>.IsConcurrencyToken()</code> so I've added that on myself, but still no exceptions are thrown in situations that should suffer concurrency issues. It instead simply over-writes the data.</p>
  
  <p>Is there something I am missing?</p>
  
  <p>Edit:</p>
  
  <p>I'm using a Database-first approach (so no [Timestamp] or any other annotations), and my DbContext is being injected into a service, configured with <code>services.AddScoped&lt;IPoRepository, PoRepository&gt;()</code> in Startup.cs</p>
  
  <p>It generates a <code>public byte[] VersionCol { get; set; }</code> field in my model, which is right I believe.</p>
  
  <p>In my PoRepository I'm trying to update my Po with the following:</p>
  
  <pre><code>public void SavePo(PoListing poListing) {
      Po po;
  
      try {
          po = _context.Po.Where( p =&gt; p.Poid == poListing.PoId ).First();
      } catch ( ArgumentNullException ) {
          throw new ArgumentNullException( "The PO does not exist." );
      }
  
      po.AssignedUserId = poListing.AssignedUserId;
      po.VersionCol     = poListing.VersionCol;
  
      _context.Entry( po ).State = EntityState.Modified;
  
      _context.SaveChanges();
  }
  </code></pre>
  
  <p>The PoListing is essentially just a part of a Po, so It only has some of it's columns (it's not a table in the database), and it has the VersionCol of the Po when it is first generated. If the PoListing has an older VersionCol than the Po it's based off, then it should give an exception.</p>
  
  <p>Edit2:</p>
  
  <p>This works, but I can't figure out how to make it work without needing to make this second context, and just use the injected context.</p>
  
  <pre><code>public void SavePo(PoListing poListing) {
      DbContextOptionsBuilder&lt;TMS_1000Context&gt; options = new DbContextOptionsBuilder&lt;TMS_1000Context&gt;();
      options.UseSqlServer( "Server=DEVSQL16;Database=TMS_1000_Dev;Trusted_Connection=True;MultipleActiveResultSets=true" );
  
      TMS_1000Context context1;
  
      try {
          po = _context.Po.Where( p =&gt; p.Poid == poListing.PoId ).First();
      } catch ( ArgumentNullException ) {
          throw new ArgumentNullException( "The PO does not exist." );
      }
  
      using ( context1 = new TMS_1000Context( options.Options ) ) {
          po.AssignedUserId = poListing.AssignedUserId;
          po.VersionCol = poListing.VersionCol;
  
          context1.Update( po );
  
          context1.SaveChanges();
      }
  }
  </code></pre>
  
  <p>Edit3:</p>
  
  <p>This is currently working. Is there another way?</p>
  
  <pre><code>public void SavePo(PoListing poListing) {
      Po po;
  
      try {
          po = _context.Po.Where( p =&gt; p.Poid == poListing.PoId ).First();
      } catch ( ArgumentNullException ) {
          throw new ArgumentNullException( "The PO does not exist." );
      }
  
      po.AssignedUserId = poListing.AssignedUserId;
  
      _context.Entry( po ).Property( u =&gt; u.VersionCol ).OriginalValue = poListing.VersionCol;
  
      _context.Update( po );
      _context.SaveChanges();
  }
  </code></pre>