title: Database concurrency exception
body: <p>we're working on a database for our study project, and we've got unexpected error while adding data to a table.</p>
  
  <pre><code>using (var ctx = new BazyDanychContext())
  {
      Osoba tmpTask = new Osoba { Imie = ImieLbl.Text, Nazwisko = NazwiskoLbl.Text, Telefon = TelefonLbl.Text, Adres = AdresLbl.Text, Mail = MailLbl.Text, IloscTransakcji = Int32.Parse(IloscLbl.Text), Typ = TypList.Text };
      ctx.Osoba.Add(tmpTask);
      ctx.SaveChanges();
  }
  </code></pre>
  
  <p>We also tried:</p>
  
  <ul>
  <li><p>To add new records using ExecuteSqlCommand, which worked just fine</p>
  
  <pre><code>ctx.Database.ExecuteSqlCommand("INSERT INTO dbo.Osoba VALUES('0','jan','nowak', '222222', 'adres', 'mail', '2', 'osoba')");
  </code></pre></li>
  <li><p>Using <code>ctx.Entry(tmpTask).State = System.Data.Entity.EntityState.Added;</code> (and also <code>.Modified</code>)</p></li>
  <li><p>Typing the values manually (like in the ExecuteSqlCommand line)</p></li>
  </ul>
  
  <p>No matter what we do, <code>ctx.SaveChanges()</code> gives us</p>
  
  <pre><code>OptimisticConcurrencyException: Store update, insert, or delete statement affected an unexpected number of rows (0).
  </code></pre>
  
  <p>Our classes:</p>
  
  <pre><code>namespace BazyDanych
  { 
      public class BazyDanychContext : DbContext
      {
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;();
              base.OnModelCreating(modelBuilder);
          }
          public BazyDanychContext() : base("ProjektBD")
          {
              Database.SetInitializer(new SQLdb());
          }
  
          public DbSet&lt;Osoba&gt; Osoba { get; set; }
      }
  }
  
  namespace BazyDanych
  {
      public class Osoba
      {
          public int ID { get; set; }
          public string Imie { get; set; }
          public string Nazwisko { get; set; }
          public string Telefon { get; set; }
          public string Adres { get; set; }
          public string Mail { get; set; }
          public int IloscTransakcji { get; set; }
          public string Typ { get; set; }
  
          public override string ToString()
          {
              return "Imie: " + Imie + "\t Nazwisko: " + Nazwisko + "\t ID: " + ID.ToString();
          }
      }
  }
  </code></pre>
  
  <p>Database table's structure looks like:</p>
  
  <pre><code>ID (int, null)
  Imie (text, null)
  Nazwisko (text, null)
  Telefon (text, null)
  Adres (text, null)
  Mail (text, null)
  IloscTransakcji (int, null)
  Typ (text, null)
  </code></pre>