title: How to tell Entity Framework that my ID column is auto-incremented (AspNet Core 2.0 + PostgreSQL)?
body: <p>Code is simple.
  <code>Tag.cs</code> entity:</p>
  
  <pre><code>public partial class Tag
  {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int Id { get; set; }
      public string Name { get; set; }
      public string Description { get; set; }
  }
  </code></pre>
  
  <p><code>HomeController.cs</code> class:</p>
  
  <pre><code>public async Task&lt;IActionResult&gt; Index()
  {
     tagRepository.Insert(new Tag 
                 { 
                     Name = "name", 
                     Description = "description" 
                 });
     await UnitOfWork.SaveChangesAsync();  // calls dbContext.SaveChangesAsync()
  
     return View();
  }
  </code></pre>
  
  <p><code>TagRepository.cs</code> class:</p>
  
  <pre><code>    // Context it's a usual DBContext injected via Repository's constructor
      public virtual void Insert(TEntity item)
                  =&gt; Context.Entry(item).State = EntityState.Added;
  </code></pre>
  
  <p><code>Tag</code> table was created by running:</p>
  
  <pre><code>CREATE TABLE Tag (
      ID SERIAL PRIMARY KEY,
      Name text NOT NULL,
      Description text NULL
  );
  </code></pre>
  
  <p>When run my application I get an error:</p>
  
  <pre><code>fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
        Failed executing DbCommand (28ms) [Parameters=[@p0='?', @p1='?', @p2='?'], CommandType='Text', CommandTimeout='30']
        INSERT INTO "tag" ("id", "description", "name")
        VALUES (@p0, @p1, @p2);
  Npgsql.PostgresException (0x80004005): 23505: duplicate key value violates unique constraint "tag_pkey"
     at Npgsql.NpgsqlConnector.&lt;DoReadMessage&gt;d__157.MoveNext()
  --- End of stack trace from previous location where exception was thrown ---
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
     at System.Runtime.CompilerServices.ValueTaskAwaiter`1.GetResult()
     at Npgsql.NpgsqlConnector.&lt;ReadMessage&gt;d__156.MoveNext()
  </code></pre>
  
  <p>As you can see Entity Framework tries to send <code>id=0</code> value to the DB, but there is already a record with <code>id=0</code> in my DB and that's why it throws <code>duplicate key</code> error.</p>
  
  <p>I didn't find any answer so far and my question is: how can I get rid of this error and tell Entity Framework that my <code>id</code> column is auto-incremented and there is no need to update it?</p>