title: Blazor / Entity Framework - Update parent when child collection is added to
body: <p>I'm using a <code>Blazor</code> server side app with <code>Entity Framework</code>.</p>
  
  <p>I have a parent entity <code>Booking</code> with a child collection of <code>Tours</code>. I have a page where I add tours to Booking's <code>Tours</code> <em>collection</em>.</p>
  
  <pre><code>public class Booking
  {
     IEnumerable&lt;Tours&gt; Tours { get; set; }
  }
  </code></pre>
  
  <p>I have a main parent booking component, which is just a shell for the booking sub-components, like Tours. I pass the child collection to the child component like so on the booking page:</p>
  
  <pre><code>&lt;Tours Tours="Booking.Tours" /&gt;
  
  @code {
      protected override async Task OnInitializedAsync()
      {
          Booking = await BookingService.GetBooking(reference);
      }
  }
  </code></pre>
  
  <p>However I find that, when I add a <strong>Tour</strong>, the page does not update with the newly added Tour unless I make a call to fetch the Booking with its children again.</p>
  
  <pre><code>private async Task addTour(TourState state)
  {
      var newBookingTour = new BookingTour();
  
       var newTour = await BookingTourService.CreateBookingTour(newBookingTour);        
  
       // UI ONLY UPDATES WHEN I DO THIS
  
       Booking = await BookingService.GetBooking();
  }
  </code></pre>
  
  <p>Here is the generic <code>method</code> I use to save entities to the <code>DB</code>:</p>
  
  <pre><code>public async Task&lt;TEntity&gt; Upsert(TEntity item)
  {
      var entity = this.Context.Update(item);
      await this.SaveChangesAsync();
      return entity.Entity;
  }
  </code></pre>
  
  <p>Should I really be making a call to the <code>DB</code> again? Surely <code>Entity Framework</code> should pick up the newly added entity and feed it through to the <code>UI</code>? I feel I'm missing something here! </p>
  
  <p>Any help appreciated as I'm new to both <code>EF</code> and <code>Blazor</code>.</p>