title: EntityFramework Core auto generate key id property
body: <p>Using .Net Core MVC.  I need to setup my model so that EntityFrameworkCore will auto generate the ID property.  I thought that just adding the <code>[Key]</code> annotation would do this, but it doesn't.  As a matter of fact, it produces an error when the ID field is left blank, which is always because I'm hiding it from the user.  </p>
  
  <p>I need it to instead Autogenerate a unique ID for the model every time.  How do I go about doing this? My ID property is currently of type <code>int</code>.</p>
  
  <p><strong>ExampleModel</strong></p>
  
  <pre><code>public class ExampleModel
  {
      [Key]
      public int ID { get; set; }
      public string SomeData { get; set; }
  }
  </code></pre>
  
  <p><strong>ExampleView</strong></p>
  
  <pre><code>@model MyNamespace.Models.ExampleModel
  
  &lt;form asp-action="Create" asp-controller="ExampleModel"&gt;
      @Html.EditorForModel()
      &lt;input type="submit" name="Submit" value="Create"/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>ExampleController</strong></p>
  
  <pre><code>public IActionResult Create ()
  {
      return View();
  }
  public IActionResult Create (ExampleModel model)
  {
      if (ModelState.IsValid)
      {
          _context.ExampleModels.Add(model);
          _context.SaveChanges();
          return RedirectToAction("Index");
      }
  
      return View(model);
  }
  </code></pre>