title: Entity Framework Core: Adding an object with a List to a DbContext results in an empty List
body: <p>We are attempting to save an Entity Framework Model to the database. </p>
  
  <p>In the model a Track always contains a Message and a Message MAY have any number of Messages in its List of Retransmits.</p>
  
  <p>When saving the model, we check if the Track model already exists in the database; If not, we simply add the Track to the DbContext. </p>
  
  <pre><code>public class Track
  {
      [Key]
      public Guid TrackId { get; set; }
      public virtual Message Message { get; set; }
  }
  
  public class Message
  {
      [Key]
      public Guid MessageId { get; set; }
      public Guid TrackId { get; set; }
  
      public virtual List&lt;Message&gt; Retransmits { get; set; }
  }
  
  public void Save (Track track)
  {
      using (var context = new DatabaseContext())
      {
          Track foundTrack = Read(track.TrackId); // Returns a Track if it already exists in the database
  
          if (foundTrack == null)
          {
              context.Add(track);
          }
          else
          {
              // Update the Track. Not relevant to this question
          }
  
          context.SaveChanges();
      }
  }
  
  // Returns a Track if it exists in the database or null
  public Track Read(Guid trackId)
  {
      using (var context = new DatabaseContext())
      {
          return context.Tracks
              .Include(t =&gt; t.Message)
                  .ThenInclude(m =&gt; m.Retransmits)
                  .FirstOrDefault(t =&gt; t.TrackId == trackId);
      }
  }
  </code></pre>
  
  <p>When adding Track to the DbContext Entity Framework will always clear the List of Retransmits. This means that a Message can contain one Message in its List of Retransmits before calling DbContext.Add, but after calling DbContext.Add it will suddenly have 0. Entity Framework is effectively clearing the list and not adding it to the database.</p>
  
  <p>We expected that the List of Retransmits would be added to the DbContext and NOT deleted from the model without warning.</p>
  
  <p>During debugging we discovered that changing the List of Retransmits from <code>List&lt;Message&gt;</code> to <code>List&lt;AnyOtherObject&gt;</code> returned the expected results and rows in the database.</p>
  
  <p>It seems that because the List of Retransmits is of the same type as the class it is contained within (I.e. A Message can contain many Messages), Entity Framework treats it differently.</p>
  
  <p>We have also tried to configure a One-to-Many relationship between the entities where <code>modelBuilder.Entity&lt;Message&gt;().HasMany(m =&gt; m.Retransmits)</code> makes no difference. And even
  <code>modelBuilder.Entity&lt;Message&gt;().HasMany(m =&gt; m.Retransmits).WithOne(m =&gt; m.Parent)</code>
  where Parent is a reference back to the containing object of a retransmitted Message. However, this resulted in a StackOverflow exception.</p>
  
  <p>What could be the reason for Entity Framework refusing to add the list to the database? Is there any way to configure Entity Framework to allow a Message to contain a List of Retransmits(being of type Message) and have it added to the database as expected? Or are we doing something wrong entirely?</p>
  
  <p><a href="https://i.stack.imgur.com/rHv0q.png" rel="nofollow noreferrer">I have attached an image of the behavior in Visual Studio's Debugger.</a></p>
  
  <p>EDIT: We have tried the latest stable Entity Framework Core version 2.2.6, as well as Entity Framework Core version 3.0.0 - Preview 9. The same behavior exists across both versions.</p>
  
  <p>EDIT EDIT: The following code initializes the model and should reproduce the issue.</p>
  
  <pre><code>public void ReproduceIssue ()
  {
      // Create a Track and Message
      Track track = new Track()
      {
          Message = new Message()
      };
  
      track.Message.TrackId = track.TrackId;
  
      // Create a Retransmitted Message
      Message retransmit = new Message()
      {
          TrackId = track.TrackId
      };
  
      // Add the Retransmitted Message to the List
      track.Message.Retransmits.Add(retransmit);
  
      Save(track);
  }
  </code></pre>