title: How does one implement C# code-first computed-column auto-update
body: <p>I have a C# Entity Framework code-first Computed-column issue that I cannot solve by viewing any of the other solutions I've found on the web.
  I believe this is because my issue has to do with a calculation
  that must be made after a row is created.</p>
  
  <p>Consider this simplified example (code-first):</p>
  
  <pre><code>public class Parent
  {
      [Key] public long Id;
      public int ExpectedChildCount { get; set; }
  
      [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
      public int ActualChildCount { get; private set; }
  
      public virtual ICollection&lt;Child&gt; Children { get; set; }
  }
  </code></pre>
  
  <pre><code>public class Child
  {
      [Key]  public long Id;  
      public long ParentId { get; set; }  // FK
      public virtual Parent Parent { get; set; }
  }
  </code></pre>
  
  <p>Doing "Add-Migration ChildCountComputed" yields the following, 
  in which I modified the Up() method:</p>
  
  <pre><code>public partial class ChildCountComputed : DbMigration
  {
          public override void Up()
          {
              //AlterColumn("Parent", "ActualChildCount", c =&gt; c.Int(nullable: false));
              Sql("UPDATE Parent SET Parent.ActualChildCount = (SELECT COUNT(*) FROM Child c WHERE c.ParentId = Parent.Id)" );
  
          }
  
          public override void Down()
          {
              AlterColumn("dbo.ServerArchive", "ActualObjectInstanceCount", c =&gt; c.Int(nullable: false));
          }
  }
  
  </code></pre>
  
  <p>I confirmed that the UPDATE command is valid and does what is expected 
  (update the Parent.ActualChildCount for all <em>already exisiting</em> Parent rows)
  when run from, say, LINQPad.</p>
  
  <p>However, when a Program creates a Parent for the first time, it may have no 
  Children. Later, they get added. But the Parent.ActualChildCount never gets
  auto updated.</p>
  
  <p>How can I get that column (Parent.ActualChildCount) to be auto-updatated 
  whenever Children are added to a Parent?</p>
  
  <p>Am I incorrect in thinking it can be done via a Computed column?</p>
  
  <p>In addition, I wonder if the 'ActualChildCount.get()' method needs to do 
  something? (but that would defeat the purpose of a Computed column, right?)</p>