Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent NHibernate HasMany not updating the FK
    text
    copied!<p>I'm using latest Fluent NHibernate lib (0.1.0.452) and I have a problem with saving child entitites.</p> <p>I think this is rather common scenario... I've got a parent with mapping:</p> <pre><code>HasMany&lt;Packet&gt;(x =&gt; x.Packets) .Cascade.All() .KeyColumnNames.Add("OrderId"); </code></pre> <p>and a simple Packet class that (in a domain model and FNH mapping) doesn't have any reference to the parent. What gets generated is a correct Packets table that contains a column named OrderId. What doesn't work is the saving. Whenever I try to save parent object, the children are also saved, but the FK stays untouched. I checked the SQL and in INSERT statement the OrderId doesn't even appear!</p> <pre><code>INSERT INTO KolporterOrders (CargoDescription, SendDate, [more cols omitted] ) VALUES ('order no. 49', '2009-04-22 00:57:44', [more values omitted]) SELECT LAST_INSERT_ID() INSERT INTO Packets (Weight, Width, Height, Depth) VALUES ('To5Kg', 1, 1, 1) SELECT LAST_INSERT_ID() </code></pre> <p>As you see the OrderId is completely missing in the last INSERT.</p> <p>I also checked the generated NH mapping and it seems it's ok:</p> <pre><code>&lt;bag name="Packets" cascade="all"&gt; &lt;key column="OrderId" /&gt; &lt;one-to-many class="Company.Product.Core.Packet, Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /&gt; &lt;/bag&gt; </code></pre> <p>I tried setting Cascade to different values. I even added References to the PacketMap (FNH mapping class).</p> <p>Any ideas why the OrderId is not being inserted?</p> <p>Edit: forgot to mention: I'm using MySQL5 if it matters.</p> <p>Edit2: The above FNH mapping generates hbm with bag (not a set) - I edited it. The C# code used for saving:</p> <pre><code>var order = new Order(); NHSession.Current.SaveOrUpdate(order); //yes, order.Packets.Count == 1 here ///Order.cs, Order ctor public Order() { CreateDate = DateTime.Now; OrderState = KolporterOrderState.New; Packets = new List&lt;Packet&gt;(); Packets.Add(new Packet() { Depth = 1, Height = 1, Width = 1, Weight = PacketWeight.To5Kg }); } </code></pre> <p>the session gets flushed and closed at EndRequest.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload