title: Child object not saving into the DB, using Entity Framework Code First
body: <p>I have a parent object which has the collection of the child objects.
  I want to save this data into the DB using EF Code First.
  For some strange reason the parent object gets saved but the child objects not.</p>
  
  <p>Here's my code:</p>
  
  <pre><code>class Person
  {
          public Person()
          {
              Operations = new ICollection&lt;Operation&gt; ();
          }
  
      public int Id { get; set; }
  
      public virtual ICollection&lt;Operation&gt; Operations { get; set; }
  }
  
  
  public class Operation
  {
      public int Id { get; set; }
  
      public string Data { get; set; }
  
      public int PersonId { get; set; }
  
      public virtual Person Person{ get; set; }
  }
  </code></pre>
  
  <p>Here's thee DAL UpdateMethod:</p>
  
  <pre><code>public bool UpdatePerson entity)
  {
  
     //_context.Set&lt;Person&gt;().AddOrUpdate(entity);
     var entityInDb = _context.Persons.Find(entity.Id);
  
     _context.Entry(entityInDb).CurrentValues.SetValues(entity);
     _context.Entry(entityInDb).State = EntityState.Modified;
  
     return _context.SaveChanges() &gt; 0;
  }
  </code></pre>
  
  <p>And here's the BLL:</p>
  
  <pre><code>   //Create a new person if it does not exist. If exists, add only the Operations:
  
         //check if such the person exists in the DB
         var person = Repository.Filter(p=&gt;p.Id == Id)
                      .AsNoTracking().FirstOrDefault();
  
  
          //if not, create a new record
          if(person == null)
          {
              personNew = new Person{
                   ...
              };
              bool res = Repository.Insert(personNew );
  
              if (res)
              {
                  //find newly created person
                  person = Repository.Filter(p =&gt; p.Id == ...)
                        .AsNoTracking().FirstOrDefault();
              }
          }
  
  //Add Operations to the Person entity and Save them:    
          var op = obj.ToOperationModel();
          op.PersonId = person.Id; 
          person.Operations.Add(op);
  
          var res2 = Repository.Update(person);
  </code></pre>
  
  <p>There is no error, the parent object (Person) gets created, res2 returns true, but no Operation is added into the DB</p>