title: code first database : SqlException: Violation of PRIMARY KEY constraint 'PK_dbo.PrivatKasses'.
body: <p>I have a model like this:</p>
  
  <pre><code>class PrivatKasse
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.None)]
      public int ID { get; set; }
      public decimal Betrag { get; set; }
      public int BenutzerID { get; set; }
  
  }
  </code></pre>
  
  <p>and a function that checks if the table is empty. if yes functions adds new rows. The function:</p>
  
  <pre><code>public static void DatabaseChecker()
      {
          using (var _db = new DataContext())
          {
              if (!_db.KasseGemeinsam.Any())
              {
                  _db.KasseGemeinsam.Add(new Models.Data.KasseGemeinsamModel() { Jahr = DateTime.Now.Year.ToString(), Monat = MonatenVertauchen.ZahlZuMonaten(Convert.ToInt32(DateTime.Now.Month)), Haushalt = 0, Hygine = 0, Mobel = 0, Nahrung = 0, Schreibware = 0, Sonstiges = 0 });
                  _db.SaveChanges();
              }
  
              if (!_db.KassePrivat.Any())
              {
                  _db.KassePrivat.Add(new Models.Data.KassePrivatModel() { Jahr = DateTime.Now.Year.ToString(), Monat = MonatenVertauchen.ZahlZuMonaten(Convert.ToInt32(DateTime.Now.Month)), UserID = UserIdentity.UserID, Fahrkosten = 0, Hygine = 0, Mobel = 0, Nahrung = 0, Schreibware = 0, Sonstiges = 0 });
                  _db.SaveChanges();
              }
  
              if (!_db.GemeinsamKasse.Any())
              {
                  _db.GemeinsamKasse.Add(new Models.Data.GemeinsamKasse() { Betrag = 0 });
                  _db.SaveChanges();
              }
  
              if (!_db.PrivatKasse.Any())
              {
                  List&lt;int&gt; userIDs = new List&lt;int&gt;();
                  foreach (var item in _db.UsersTbl)
                  {
                      userIDs.Add(item.ID);
                  }
                  using (var _db2 = new DataContext())
                  {
                      foreach (var item in userIDs)
                      {
                          _db2.PrivatKasse.Add(new Models.Data.PrivatKasse() { BenutzerID = item, Betrag = 0 });
  
                      }
                      _db2.SaveChanges();
                  }
  
  
  
              }
  
  
          }
  </code></pre>
  
  <p>But I get an SqlExeption : </p>
  
  <blockquote>
    <p>System.Data.Entity.Infrastructure.DbUpdateException: "An error occurred while updating the entries. See the inner exception for details." </p>
    
    <p>UpdateException: An error occurred while updating the entries. See the
    inner exception for details.</p>
    
    <p>SqlException: Violation of PRIMARY KEY constraint
    'PK_dbo.PrivatKasses'. Cannot insert duplicate key in object
    'dbo.PrivatKasses'. The duplicate key value is (0).</p>
    
    <p>The statement has been terminated.</p>
  </blockquote>
  
  <p>How can i fix this problem?</p>