title: ASP.NET with Entity Framework: EF does not generate identity automatically
body: <p>I'm quite new to ASP and I'm creating a simple ASP.NET MVC App that stores personal info in a database. Using a DB First approach, I created the following db table:</p>
  
  <pre><code>CREATE TABLE [dbo].[Author] (
  [PersonID]  INT           IDENTITY (1, 1) NOT NULL,
  [MobileNum] NVARCHAR (50) NULL,
  [Location]  NVARCHAR (50) NULL,
  [LinkedIn]  NVARCHAR (50) NULL,
  [FaceBook]  NVARCHAR (50) NULL,
  [Picture]   IMAGE         NULL,
  [Name]      NVARCHAR (50) NULL,
  [Email]     NVARCHAR (50) NULL,
  PRIMARY KEY CLUSTERED ([PersonID] ASC)
  </code></pre>
  
  <p>);</p>
  
  <p>I use <em>PersonID</em> as a <code>PrimaryKey</code> and I want EF to autogenerate this for me, but for some reason this does not happen.
  I adjusted the identity options manually among the properties:</p>
  
  <p><a href="https://i.stack.imgur.com/1KUlC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1KUlC.png" alt="PersonID marked as identity"></a></p>
  
  <p>and I also attached the necessary attributes to my model (although I read somewhere that autogeneration is supposed to work without them as well):</p>
  
  <pre><code>public partial class Author
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int PersonID { get; set; }
      public string MobileNum { get; set; }
      public string Location { get; set; }
      public string LinkedIn { get; set; }
      public string FaceBook { get; set; }
      public byte[] Picture { get; set; }
      public string Name { get; set; }
      public string Email { get; set; }
  }
  </code></pre>
  
  <p>Yet, the <code>ModelState</code> of the db is always false during the creation of a new entry:</p>
  
  <p><a href="https://i.stack.imgur.com/Zyo6V.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Zyo6V.png" alt="ModeslState is false"></a></p>
  
  <p>and taking a closer look at it in the debugger, the problem seems to be that EF expects <em>PersonID</em> to be assigned by the user.</p>
  
  <p>I have the following <code>View</code> belonging to the Create Action:</p>
  
  <pre><code>&lt;div class="form-group"&gt;
      @Html.HiddenFor(model =&gt; model.PersonID, htmlAttributes: new { @class = "control-label col-md-2" })
  &lt;/div&gt;
  </code></pre>
  
  <p>Is there something else I'm supposed to do to enable the autogeneration of PersonIDs? What am I missing? Pointing me to a beginner level, DB First article is also much appreciated, since most articles I found did not make any mention of this problem whatsoever (e.g., <a href="https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/setting-up-database" rel="nofollow noreferrer">here</a>, <a href="http://www.c-sharpcorner.com/UploadFile/8ef97c/mvc-5-0-application-using-entity-framework-db-first-approach/" rel="nofollow noreferrer">here</a>, or <a href="https://www.codeproject.com/Articles/1209903/Getting-Started-with-Entity-Framework-Core-Databas" rel="nofollow noreferrer">here</a>).</p>