title: One-to-many relationship getting error in Entity Framework Core when trying to insert data
body: <p>I'm trying to add data to one-to-many relationship tables. But there is an exception.</p>
  
  <p>There are two tables:</p>
  
  <ol>
  <li>Post</li>
  <li>Attachment</li>
  </ol>
  
  <p>The Post has many Attachment but one Attachment has one unique post. I'm going to try:</p>
  
  <ol>
  <li>Adding records to the <code>Post</code> table, then after that</li>
  <li>using that post-Id. update the <code>Attachment</code> table</li>
  </ol>
  
  <p>Here is the exception thrown</p>
  
  <blockquote>
    <p>InvalidOperationException: The property 'Id' on entity type 'Posts' has a temporary value. Either set a permanent value explicitly or ensure that the database is configured to generate values for this property.</p>
    
    <p>at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.Validate(ModificationCommand modificationCommand)<br>
    at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.d__8.MoveNext()<br>
    at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(Tuple<code>2 parameters)<br>
    at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func</code>3 operation, Func<code>3 verifySucceeded)
           at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func</code>2 operation)
           at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(IEnumerable<code>1 commandBatches, IRelationalConnection connection)
           at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IReadOnlyList</code>1 entries)
           at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IReadOnlyList`1 entriesToSave)
           at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
           at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
           at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
           at GradChat.Data.Repo.PostRepository.PostRepo.AddPost(Posts post) in C:\Users\kokuadmin\source\repos\GradChat\GradChat.Data.Repo\PostRepository\PostRepo.cs:line 27'</p>
  </blockquote>
  
  <p>And here the my <code>OnModelCreating()</code></p>
  
  <pre><code>protected override void OnModelCreating(ModelBuilder modelBuilder)
  {    
        modelBuilder.Entity&lt;Posts&gt;()
            .HasOne(p =&gt; p.User)
            .WithMany(c =&gt; c.Posts)
            .HasForeignKey(p =&gt; p.Id);    
  
        modelBuilder.Entity&lt;Attachment&gt;()
            .HasOne(p =&gt; p.Posts)
            .WithMany(c =&gt; c.Attachment)
            .HasForeignKey(p =&gt; p.Id);    
      }    
  }
  </code></pre>
  
  <p>And here are the two entity classes :</p>
  
  <pre><code>public class Attachment
  {
      public int Id { get; set; }    
      public string FileName { get; set; }    
      public string FileTye { get; set; }    
      public virtual Posts Posts { get; set; }    
      public int PostId { get; set; }    
  }
  
  public class Posts
  {     
      public int Id { get; set; }    
      public string Title { get; set; }    
      public string Content { get; set; }    
      public virtual User User { get; set; }    
      public int UserId { get; set; }    
      public virtual ICollection&lt;Attachment&gt; Attachment { get; set; }    
  }
  </code></pre>
  
  <p>I'm using Microsoft.EntityFramework.Core.SqlServer (2.0.1)</p>