title: Entity framework is making the id null on insert
body: <p>I'm getting the following error when I try to insert a new row in one of my relational tables. I have the following two models: </p>
  
  <pre><code>public class CompanyCredit
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int creditId { get; set; }
  
      public int planCredit { get; set; }
      public DateTime? PlanCreditExpirationDate { get; set; }
  
  }
  </code></pre>
  
  <p>And</p>
  
  <pre><code>public class CompanyInformation
  {
  
      [Key]
      [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
      public int id { get; set; }
  
      [Required]
      [DisplayName("Company Name:")]
      public string companyName { get; set; }
  
      public string timeZone { get; set; }
  
      //navigation Properties       
      public virtual CompanyCredit Credits { get; set; }
  }
  </code></pre>
  
  <p>And this Relation in the dbContext</p>
  
  <pre><code>modelBuilder.Entity&lt;CompanyInformation&gt;().HasOptional(e =&gt; e.Credits);
  </code></pre>
  
  <p>I'm trying to add a record inside CompanyCredit table like so:</p>
  
  <pre><code>if (_company.Credits == null)
  {
     var _credits = new CompanyCredit();
     _credits.planCredit = 200;
     _credits.PlanCreditExpirationDate = System.DateTime.UtcNow.AddMonths(1);
  
     _company.Credits = _credits;
     repo.InsertOrUpdate(_company, User.Identity.Name);
   }
  </code></pre>
  
  <p>And Finally Insert or update just marks Company as changed and _credit as added like so:</p>
  
  <pre><code> _db.Entry(_credits).State = System.Data.EntityState.Added;
   _db.Entry(Company).State = System.Data.EntityState.Modified;
   _db.SaveChanges();
  </code></pre>
  
  <p>When this runs I get the following Error that I just can't seem to find the reason to.</p>
  
  <pre><code> Cannot insert the value NULL into column 'creditId', table 'Project.dbo.CompanyCredits'; column does not allow nulls. INSERT fails.
  </code></pre>
  
  <p>The statement has been terminated.</p>
  
  <p>Thank in advanced for your help.</p>