title: Primary keys created by Entity Framework are not auto-incrementing
body: <p>Let me show the error:</p>
  
  <blockquote>
    <p>Cannot insert the value NULL into column 'Id', table
    'TCCDatabase.dbo.ProfessionalModels'; column does not allow nulls.
    INSERT fails.</p>
  </blockquote>
  
  <p>I got this error when trying to insert n record into my database using a database created by my EF application.</p>
  
  <p>I think it's happening because my database Id column is not auto-incrementing.</p>
  
  <p><img src="https://i.stack.imgur.com/FDoQO.png" alt="Identity field"></p>
  
  <p>My question is: Why are my Id Columns not auto-incrementing?</p>
  
  <p>One of my models:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations;
  using System.ComponentModel.DataAnnotations.Schema;
  using System.Linq;
  using System.Web;
  
  namespace TCCApplication.Models
  {
      public class ProfessionalModel
      {
          public int Id { get; set; }
  
          public string Name { get; set; }
  
          [Column(TypeName = "DateTime2")]
          public DateTime BirthDate { get; set; }
          public int PhoneNumber { get; set; }
          public string Profession { get; set; }
  
          public UserAddressModel UserAddressModel { get; set; }
          public UserAccountModel UserAccountModel { get; set; }
  
          public ProfessionalModel() { }
      }
  }
  </code></pre>
  
  <p>My context class:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations.Schema;
  using System.Data.Entity;
  using System.Linq;
  using System.Web;
  using TCCApplication.Models;
  
  namespace TCCApplication.EntityFramework
  {
      public class TCCDatabase : DbContext
      {
          public DbSet&lt;UserAccountModel&gt; UserAccountContext { get; set; }
          public DbSet&lt;ProfessionalModel&gt; ProfessionalContext { get; set; }
          public DbSet&lt;UserAddressModel&gt; UserAddressContext { get; set; }
  
          public TCCDatabase()
          {}
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;ProfessionalModel&gt;().Property(a =&gt; a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
              modelBuilder.Entity&lt;UserAccountModel&gt;().Property(a =&gt; a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
              modelBuilder.Entity&lt;UserAddressModel&gt;().Property(a =&gt; a.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          }
  
      }
  }
  </code></pre>