title: Saving one-to-many relationship in Entity Framework Core
body: <pre><code>public Master Add(Master request)
  {
      request.CreatedDate = DateTime.Now;
      // master
      Context.Entry(request).State = EntityState.Added;
      // details
      if (request.Details != null &amp;&amp; request.Details?.Count != 0)
      {
          request.Details.ForEach(x =&gt; Context.Entry(x).State = EntityState.Added);    
      }
      Context.SaveChanges();
      return request;
  }  
  </code></pre>
  
  <p>I am trying to insert master and list of details with relation one to many  using ef core,
  but an exception has been thrown in this line inside foreach.</p>
  
  <p>instance of entity type cannot be tracked because another instance with same key value is tracked</p>
  
  <p>How could i do it ?</p>
  
  <p>Thanks</p>