title: How to save a child with assigned id in nhibernate
body: <p>I have two classes:</p>
  
  <pre><code>public class Parent
  {
      public virtual long? ID { get; set; } // native
      public virtual IList&lt;Child&gt; Children { get; set; }
      public virtual string Name { get; set; }
  }
  
  public class Child
  {
      public virtual long ID { get; set; } // assigned
      public virtual string Name { get; set; }
  }
  </code></pre>
  
  <p>Instantiating and saving parent and child:</p>
  
  <pre><code>child = new Child() { ID = 1, Name = "SomeName" };
  parent = new Parent() { Children = new List() { child } };
  session.Save(parent);
  </code></pre>
  
  <p>Which gives me:</p>
  
  <p><em>NHibernate.StaleStateException: Unexpected row count: 0; expected: 1.</em></p>
  
  <p>I think the problem is with the assigned id on the child. Since it has an id, NHibernate thinks it has previously saved before which is not the case.</p>
  
  <p>The generated (trimmed &amp; renamed) SQL is:</p>
  
  <pre><code>NHibernate: select child0_.ID as child1_1_, child0_.NAME as NAME1_, child0_.PARENT_ID as COMMAND7_1_, from CHILD child0_
  NHibernate: select parent0_.PARENT_ID as parent1_10_
  NHibernate: select parent0_.PARENT_ID as parent1_10_, parent0_.NAME as parent2_10_ from PARENT parent0_
  NHibernate: UPDATE CHILD SET PARENT_ID = @p0 WHERE CHILD_ID = @p1;@p0 = 2, @p1 = 1
  </code></pre>
  
  <p>Mapping files:</p>
  
  <pre><code>&lt;class name="MyNamespace.Child" table="CHILD"&gt;
    &lt;id name="ID" column="CHILD_ID" type="System.Int64"&gt;
      &lt;generator class="assigned"&gt;&lt;/generator&gt;
    &lt;/id&gt;
    &lt;property name="Name" column="NAME"&gt;&lt;/property&gt;
  &lt;/class&gt;
  
  &lt;class name="MyNamespace.Parent" table="PARENT"&gt;
    &lt;id name="ID" column="PARENT_ID" type="System.Int64"&gt;
      &lt;generator class="native"&gt;&lt;/generator&gt;
    &lt;/id&gt;
    &lt;property name="Name" column="NAME"&gt;&lt;/property&gt;
    &lt;bag name="Children"&gt;
      &lt;key column="PARENT_ID"&gt;&lt;/key&gt;
      &lt;one-to-many class="MyNamespace.Child"&gt;&lt;/one-to-many&gt;
    &lt;/bag&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>While searching google, I found about version tag which may be a solution but I do not have a persistent field to use as version. In this case, how can I save (insert) a child with assigned id and its parent?</p>