title: Can't delete child element with Entity Framework
body: <p>Can someone please help me with my problem? I searched on the internet a lot. But still can't solve it.</p>
  
  <p>I'm using Entity Framework 6 code-first with Microsoft SQL Server. </p>
  
  <p>My entities are like this:</p>
  
  <pre><code>public class StockInfo
  {
      [JsonIgnore]
      [Key]
      public int StockInfoID { get; set; }
      [ForeignKey("StockInfoID")]
      public virtual CompanyInfo companyInfo { get; set; }
      [ForeignKey("StockInfo_ID")]
      public virtual List&lt;CorpAction&gt; corpActions { get; set; }
      ...... so on
  }
  
  public class CompanyInfo
  {
      [JsonIgnore]
      // [Key, ForeignKey("StockInfo")]
      public int CompanyInfoID { get; set; }
      public int avgVolume { get; set; }
      public string coName { get; set; }
      ...........
  }
  
  public class CorpAction
  {
      public int ID { get; set; }
      public int corpActionType { get; set; }
      public string date { get; set; }
      ........
  
      public int StockInfo_ID { get; set; }
  }
  </code></pre>
  
  <p>Then I try to save/update <code>StockInfo</code> in my repository:</p>
  
  <pre><code>public void SaveStockInfo(StockInfo StockWeeklyInfo)
  {
      var content = context.StockInformation.Where(x =&gt; x.companyInfo.symbol == StockWeeklyInfo.companyInfo.symbol).FirstOrDefault();
  
      if (content == null)
      {
          context.StockInformation.Add(StockWeeklyInfo);
      }
      else
      {
          context.StockInformation.Remove(content);
          context.Entry(content).State = EntityState.Deleted;
          context.StockInformation.Add(StockWeeklyInfo);
      }
  
      context.SaveChanges();
  }
  </code></pre>
  
  <p>My problem is that when I remove <code>StockInfo</code> and add a new one. In SQL Server, I can see a new <code>StockInfo</code> entity instead of the old one.</p>
  
  <p>But in the <code>CompanyInfo</code> table, Entity Framework just add another one and doesn't delete the previous. </p>
  
  <p>What I am doing wrong? Interestingly, <code>CorpAction</code> works well.</p>