title: EF Core: Column with ValueGeneratedOnAdd trows "Cannot insert explicit value for identity column..."
body: <p>I have ASP.NET MVC 5 application that uses Entity Framework 6.2. This app creates some records in MS SQL server.</p>
  
  <p>I have to process this records in AWS lambda function. For this purpose, I created c# .NET Core v2.0 lambda function. There I generate models from existed DB as described in the next article - <a href="https://docs.microsoft.com/en-us/ef/core/get-started/aspnetcore/existing-db" rel="nofollow noreferrer">Getting Started with EF Core on ASP.NET Core with an Existing Database</a>.</p>
  
  <p>Generated models look like:</p>
  
  <pre><code>public partial class Request
  {
      public int EntityId { get; set; }   
      public Result Result { get; set; }
  }
  public partial class Result
  {
      public int EntityId { get; set; }
      public Request Entity { get; set; }
  }
  </code></pre>
  
  <p>And OnModelCreating:</p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      modelBuilder.Entity&lt;Request&gt;(entity =&gt;
      {
          entity.HasKey(e =&gt; e.EntityId);
          entity.ToTable("Request", "mySchema");
      });
  
      modelBuilder.Entity&lt;Result&gt;(entity =&gt;
      {
          entity.HasKey(e =&gt; e.EntityId);
          entity.ToTable("Result", "mySchema");
          entity.HasIndex(e =&gt; e.EntityId).HasName("IX_EntityId");
          entity.Property(e =&gt; e.EntityId).ValueGeneratedOnAdd();
          entity.HasOne(d =&gt; d.Entity)
              .WithOne(p =&gt; p.Result)
              .HasForeignKey&lt;Result&gt;(d =&gt; d.EntityId)
              .OnDelete(DeleteBehavior.ClientSetNull)
              .HasConstraintName("FK_mySchema.Result_mySchema.Request_EntityId");
      });
  }
  </code></pre>
  
  <p>In the lambda function, I am processing a request and  setting result:</p>
  
  <pre><code>using (var db = new DataContext(connection))
  {
      var request = db.Request.First();    
      request.Result = new Result();
      db.SaveChanges();
  }
  </code></pre>
  
  <p>On the save operation I got the exception:</p>
  
  <p><em>Cannot insert explicit value for identity column in table 'Result' when IDENTITY_INSERT is set to OFF.</em></p>
  
  <p>I checked different way to solve this issue. But there are already set <code>ValueGeneratedOnAdd</code> function in model creating method.</p>
  
  <p>Also, I tried to add <code>[DatabaseGenerated(DatabaseGeneratedOption.Identity)]</code> to Result.EntityId  - without any success.</p>
  
  <p>What did I miss in my implementation?</p>