title: Updating entity in EF Core application with SQLite gives DbUpdateConcurrencyException
body: <p>I try to use optimistic concurrency check in EF Core with SQLite.
  The simplest positive scenario (even without concurrency itself) gives me 
  <code>Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: 'Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded</code>.</p>
  
  <p>Entity:</p>
  
  <pre><code>public class Blog
  {
      public Guid Id { get; set; }
      public string Name { get; set; }
      public byte[] Timestamp { get; set; }
  }
  </code></pre>
  
  <p>Context:</p>
  
  <pre><code>internal class Context : DbContext
  {
      public DbSet&lt;Blog&gt; Blogs { get; set; }
  
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          optionsBuilder.UseSqlite(@"Data Source=D:\incoming\test.db");
          ///optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;");
      }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Blog&gt;()
              .HasKey(p =&gt; p.Id);
  
          modelBuilder.Entity&lt;Blog&gt;()
              .Property(p =&gt; p.Timestamp)
              .IsRowVersion()
              .HasDefaultValueSql("CURRENT_TIMESTAMP");
      }
  }
  </code></pre>
  
  <p>Sample:</p>
  
  <pre><code>internal class Program
  {
      public static void Main(string[] args)
      {
          var id = Guid.NewGuid();
          using (var db = new Context())
          {
              db.Database.EnsureDeleted();
              db.Database.EnsureCreated();
              db.Blogs.Add(new Blog { Id = id, Name = "1" });
              db.SaveChanges();
          }
  
          using (var db = new Context())
          {
              var existing = db.Blogs.Find(id);
              existing.Name = "2";
              db.SaveChanges(); // Exception thrown: 'Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException'
          }
  
      }
  }
  </code></pre>
  
  <p>I suspect it's something to do with the data types between EF and SQLite.
  Logging gives me the following query on my update:</p>
  
  <pre><code>Executing DbCommand [Parameters=[@p1='2bcc42f5-5fd9-4cd6-b0a0-d1b843022a4b' (DbType = String), @p0='2' (Size = 1), @p2='0x323031382D31302D30372030393A34393A3331' (Size = 19) (DbType = String)], CommandType='Text', CommandTimeout='30']
  UPDATE "Blogs" SET "Name" = @p0
  WHERE "Id" = @p1 AND "Timestamp" = @p2;
  </code></pre>
  
  <p>But the column types are BLOB for both Id and Timestamp (SQLite does not provide UUID and timestamp column types):</p>
  
  <p><a href="https://i.stack.imgur.com/1oV7f.png" rel="noreferrer"><img src="https://i.stack.imgur.com/1oV7f.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>At the same time if I use SQL Server (use commented connection string + remove <code>.HasDefaultValueSql("CURRENT_TIMESTAMP")</code>), sample works correctly and updates timestamp in the DB.</p>
  
  <p>Used packages:</p>
  
  <pre><code>&lt;PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.1.4" /&gt;
  &lt;PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="2.1.4" /&gt;
  </code></pre>
  
  <p>Have I configured the model for concurrency check wrong?
  That drives me crazy that I can't make it work with this simplest scenario. </p>
  
  <hr>
  
  <p><strong>UPDATE:</strong> how I finally made it work. Here only idea is shown, but probably it helps anybody:</p>
  
  <pre><code>public class Blog
  {
      public Guid Id { get; set; }
      public string Name { get; set; }
      public long Version { get; set; }
  }
  
  internal class Context : DbContext
  {
      public DbSet&lt;Blog&gt; Blogs { get; set; }
  
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
      {
          optionsBuilder.UseSqlite(@"Data Source=D:\incoming\test.db");
      }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Blog&gt;()
              .HasKey(p =&gt; p.Id);
  
          modelBuilder.Entity&lt;Blog&gt;()
              .Property(p =&gt; p.Version)
              .IsConcurrencyToken();
      }
  }
  
  internal class Program
  {
      public static void Main(string[] args)
      {
          var id = Guid.NewGuid();
          long ver;
          using (var db = new Context())
          {
              db.Database.EnsureDeleted();
              db.Database.EnsureCreated();
              var res = db.Blogs.Add(new Blog { Id = id, Name = "xxx", Version = DateTime.Now.Ticks});
              db.SaveChanges();
          }
  
          using (var db = new Context())
          {
              var existing = db.Blogs.Find(id);
              existing.Name = "yyy";
              existing.Version = DateTime.Now.Ticks;
              db.SaveChanges(); // success
          }
  
          using (var db = new Context())
          {
              var existing = db.Blogs.Find(id);
              existing.Name = "zzz";
              existing.Version = DateTime.Now.Ticks;
              db.SaveChanges(); // success
          }
  
          var t1 = Task.Run(() =&gt;
          {
              using (var db = new Context())
              {
                  var existing = db.Blogs.Find(id);
                  existing.Name = "yyy";
                  existing.Version = DateTime.Now.Ticks;
                  db.SaveChanges();
              }
          });
  
          var t2 = Task.Run(() =&gt;
          {
              using (var db = new Context())
              {
                  var existing = db.Blogs.Find(id);
                  existing.Name = "zzz";
                  existing.Version = DateTime.Now.Ticks;
                  db.SaveChanges();
              }
          });
  
          Task.WaitAll(t1, t2); // one of the tasks throws DbUpdateConcurrencyException
      }
  }
  </code></pre>