title: Entity framework - assignment to list behaviour
body: <p>Recently I spoted strange behavior in EntityFramework 6, given following program
  can anyone explain to me why </p>
  
  <pre><code>parent.Children = new List&lt;ChildObject&gt; { new ChildObject("newChild", "newValue") };
  </code></pre>
  
  <p>actually <em>adds</em> new child to list of Children instead of removing old children and adding new ones. </p>
  
  <p>Using <code>db.Parents.Include(x =&gt; x.Children).First()</code> to load parent solves problem, which makes this behavior even stranger...</p>
  
  <p>Was this conscious design choice or is it actually a bug?</p>
  
  <p>Full test program:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.ComponentModel.DataAnnotations.Schema;
  using System.Data.Entity;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace EF_PlayWithList
  {
      public class ChildObject
      {
          public int Id { get; protected set; }
  
          public int ParentId { get; protected set; }
          public string Name { get; protected set; }
          public string Value { get; protected set; }
  
          protected ChildObject() { }
  
          public ChildObject(string name, string value)
          {
              this.Name = name ?? "";
              this.Value = value ?? "";
          }
  
          public override string ToString()
          {
              return string.Format("{0}: {1}", this.Name, this.Value);
          }
      }
  
      public class ParentObject
      {
          public int Id { get; protected set; }
          public string Name { get; protected set; }
  
          public virtual IList&lt;ChildObject&gt; Children { get; set; }
  
          protected ParentObject() { }
  
          public ParentObject(string name)
          {
              this.Name = name;
              this.Children = new List&lt;ChildObject&gt;();
          }
  
          public void AddChild(ChildObject child)
          {
              this.Children.Add(child);
          }
  
          public override string ToString()
          {
              return string.Format("Parent '{0}' with {1} childs.", this.Name, this.Children.Count);
          }
      }
  
      class TestDbContext : DbContext
      {
          public DbSet&lt;ParentObject&gt; Parents { get; set; }
  
          public TestDbContext()
              : base()
          {
  
          }
  
          protected override void OnModelCreating(DbModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;ParentObject&gt;()
                  .HasMany(x =&gt; x.Children)
                  .WithRequired()
                  .HasForeignKey(x =&gt; x.ParentId)
                  .WillCascadeOnDelete();
  
              modelBuilder.Entity&lt;ParentObject&gt;()
                  .HasKey(x =&gt; x.Id);
  
              modelBuilder.Entity&lt;ParentObject&gt;()
                  .Property(x =&gt; x.Id)
                  .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
  
              modelBuilder.Entity&lt;ChildObject&gt;()
                  .HasKey(x =&gt; new { x.Id, x.ParentId });
  
              modelBuilder.Entity&lt;ChildObject&gt;()
                  .Property(x =&gt; x.Id)
                  .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
          }
      }
  
      class Program
      {
          static void Main(string[] args)
          {
               Database.SetInitializer(new DropCreateDatabaseAlways&lt;TestDbContext&gt;());
  
                  using (var db = new TestDbContext())
                  {
                      var parent = new ParentObject("superFooParent");
                      parent.AddChild(new ChildObject("foo", "1"));
                      parent.AddChild(new ChildObject("bar", "2"));
  
                      db.Parents.Add(parent);
                      db.SaveChanges();
                  }
  
                  using (var db = new TestDbContext())
                  {
                      var parent = db.Parents.First();
  
                      parent.Children = new List&lt;ChildObject&gt;
                      {
                          new ChildObject("newChild", "newValue")
                      };
  
                      db.SaveChanges();
                  }
  
                  using (var db = new TestDbContext())
                  {
                      foreach (var parent in db.Parents.Include(x =&gt; x.Children))
                      {
                          Console.WriteLine(parent);
  
                          foreach (var child in parent.Children)
                          {
                              Console.WriteLine("\t{0}", child);
                          }
                      }
                  }
  
              Console.ReadLine();
          }
      }
  }
  </code></pre>