title: EF Core adding Parent and Child in same transaction
body: <p>I have standard parent/children design:</p>
  
  <pre><code>class Parent {
      Guid Id;
      List&lt;Child&gt; Children;
  
      public Parent() {
          Id = Guid.NewGuid();
          this.Children = new List&lt;Child&gt;();
      }
  
      public AddChild(Child ch) {
          this.Children.Add(ch);
      }
  }
  
  class Child {
      Guid Id;
  
      public Child() {
          Id = Guid.NewGuid();
      }
  }
  </code></pre>
  
  <p>My mapping is like this:</p>
  
  <pre><code>public void Configure(EntityTypeBuilder&lt;Parent&gt; builder)
  {
      builder.ToTable("Parents");
      builder.HasKey(x =&gt; x.Id);
  
      builder.HasMany(p =&gt; p.Children)
          .WithOne()
          .HasForeignKey("ParentId");
  }
  
  public void Configure(EntityTypeBuilder&lt;Child&gt; builder)
  {
      builder.ToTable("Children");
      builder.HasKey(x =&gt; x.Id);
  }
  </code></pre>
  
  <p>Now, in my service method (add child to parent, create parent in case of not existing in system):</p>
  
  <pre><code>var parent = await _parentsRepository.GetParent(parentId);
  
  if (parent == null)
  {
      parent = new Parent();
      await _parentsRepository.AddAsync(customerMembersip); // adding parent to context, with empty Children collection, but not saving to DB yet
  }
  
  parent.AddChild(new Child()); // this make this new Child in Parent object to be in state "Modified", but in fact should be in state "Added". Why?
  
  // save changes here
  </code></pre>
  
  <p>In other words, why adding child to parent (parent just added to context) makes child being seen as <strong>Modified</strong>? But if I get the parent from repo and it is immediately in context, and then I add child, then child is seen as <strong>Added</strong>.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>It may be due to fact, that Id (of type Guid) is generated in C# code, thus EF "thinks" it is existing entity, thus it marks is as "Modified". But I don't quite understand why, in case of Parent already exists in DB (the _parentsRepository returns actual entity)</p>