title: Fluent NHibernate HasMany Collection Problems
body: <p><strong>Update:</strong>
  It appears that changing my mapping from Cascade.All() to Cascade.AllDeleteOrphan() fixes most of my issues. I still have to explicitly set the Company property on the OperatingState, which seems unnecessary as it's being added to the Company entity, but at least I can work with that during an update. I still need to test that with a create. </p>
  
  <p>If any one can explain that, that would be a big help.</p>
  
  <p><strong>Update 2:</strong> After playing with it some more, it appears I don't always have to specify the parent entity.</p>
  
  <p><strong>Original Post</strong></p>
  
  <p>I have 2 related entities</p>
  
  <pre><code>public class Company {
          //... fields
          public virtual IList&lt;OperatingState&gt; OperatingStates { get; set; }
  }
  
  public class OperatingState {
          public virtual Company Company { get; set; }// Mapped on CompanyID
          public virtual string State { get; set; }
  }
  </code></pre>
  
  <p>And they are mapped like this:</p>
  
  <pre><code> public class CompanyMap : ClassMap&lt;Company&gt; {
          public CompanyMap() {
          //... fields 
            HasMany(x =&gt; x.OperatingStates)
                  .Cascade.All()
                  .Table("OperatingState");
          }
  }
   public class OperatingStateMap : ClassMap&lt;OperatingState&gt; {
          public OperatingStateStateMap() {
              Id(x =&gt; x.ID);
              References(x =&gt; x.Company);
              Map(x =&gt; x.State);
          }
   }
  </code></pre>
  
  <p>So all is well until I try to update Company with new Operating States</p>
  
  <pre><code>Company company = _repo.GetSingle(123);
   company.OperatingStates.Clear();
   foreach(string state in form["OperatingStates"].Split(',')) {
      company.OperatingStates.Add(new OperatingState(state));
   }
   _repo.Save(company); // calls ISession.SaveOrUpdate
  </code></pre>
  
  <p>It bombs out with:</p>
  
  <blockquote>
    <p>Cannot insert the value NULL into
    column 'CompanyID', table
    'ConsumerCartel.dbo.CompanyOperatingState';
    column does not allow nulls. INSERT
    fails. The statement has been
    terminated.</p>
  </blockquote>
  
  <p>However, if I make 2 changes it kind of works</p>
  
  <pre><code>Company company = _repo.GetSingle(123);
   // don't clear the list this time;
   foreach(string state in form["OperatingStates"].Split(',')) {
      OperatingState os = new OperatingState(state);
      // explicitly setting the company
      os.Company = company;
      company.OperatingStates.Add(os);
   }
   _repo.Save(company); // calls ISession.SaveOrUpdate
  </code></pre>
  
  <p>Which will add the new states in addition to the old ones, which is not what I want. However, <strong>even when explicitly setting the company (which I shouldn't have to do when it's added to a mapped list?) it doesn't work if the list is cleared</strong>.</p>
  
  <p>This code has worked on other entities, but not on this one, so I think this <strong>should</strong> work as written. What am I doing wrong?</p>