title: How to update existing rows using Entity Framework?
body: <p>I have the following one to many table as below. I am experiencing issue when it comes to editing the existing rows with the following code. Please understand that I am pretty new to the ET relationship, so any detailed explanation would be greatly appreciated. Why it is returning null values?</p>
  
  <p><a href="https://i.stack.imgur.com/iuwCi.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iuwCi.png" alt="enter image description here"></a></p>
  
  <pre><code>public void UpdateReportGroup(TReportHeaderModel model)
      {
  
          if (model.THeaderTitle == null)
          {
              throw new Exception("Report Group Title must be filled in");
  
          }
  
          if (model.THeaderTitle.Length &lt;= 0)
          {
              throw new Exception("A Report Group Title must be filled in.");
  
          }
          using (var connection = new TReportEntitiesConnection())
          {
  
              var header = connection.THeaders.SingleOrDefault(f =&gt; f.ID == model.ID);
              var reports = connection.TReport.Where(f =&gt; f.THeaderID == model.ID);
              connection.TReport.RemoveRange(reports);
              foreach (var urls in model.TReports)
              {
                  connection.TReport.Add(new TReport()
  
                  {
                      TReportName = urls.name,
                      URL = urls.url,
                  });
              }
              connection.THeaders.Add(header);
              connection.SaveChanges()      
  
          }
      }
  </code></pre>
  
  <p>Everytime, I debug it,it is giving null values for the 'TReport' table.
  My create new rows works perfectly with the following code. Meaning, I am returning the correct form with correct field names. </p>
  
  <pre><code>public void CreateReport(TReportHeaderModel model)
          {
  
  
              if (model.THeaderTitle == null)
              {
                  throw new Exception("Report Group Title must be filled in");
  
              }
  
              if (model.THeaderTitle.Length &lt;= 0)
              {
  
                  throw new Exception("A Report Group Title must be filled in.");
  
              }
  
                  using (var connection = new TReportEntitiesConnection())
  
                  {
                      var header = new THeader()
                      {
                          ClientID = model.ClientID,
  
                          THeaderTitle = model.THeaderTitle,
                          RowNumber = model.RowNumber
                      };
  
  
                      foreach (var urls in model.TReports)
                      {
  
                          header.TReports.Add(new TReport()
                          {
  
                              TReportName = urls.name,
                              URL = urls.url
  
  
                          });
  
                      }
  
                      connection.THeaders.Add(header);
                      connection.SaveChanges();
  
                  }
  
          }
  </code></pre>
  
  <p>As you can see, I am following DI pattern, and therefore I am calling these two methods in my controller as below: </p>
  
  <pre><code>[HttpPost]
          public ActionResultModel CreateReportAPI([FromBody] TReportHeaderModel model) //attempt 3
          {
  
              try {
                  if (ModelState.IsValid)
                  {
                      var isValid = _tReportingService.HeadernameExists(model.THeaderTitle);
  
                      if (!isValid)
  
                      {
                          Console.WriteLine("it does not exist");
                          var user = this.GetCurrentUserAccount();
                          model.ClientID = user.SelectedClient.ID;
  
                          _tReportingService.CreateReport(model);
  
                      }
  
                      else //Update method comes till here and it goes //straight to the error
                      {
  
                          Console.WriteLine("it exists");
                          var user = this.GetCurrentUserAccount();
                          model.ClientID = user.SelectedClient.ID;
  
                          _tReportingService.UpdateReportGroup(model);
  
                      }
                  }
  
  
                  return new ActionResultModel()
                  {
  
                      Success=true, 
                      Message="Report Group Successfully Saved."
  
  
                  };
  
  
  
  
              }
  </code></pre>