Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy isn't my NHibernate bag collection setting the 'parent id' of the children dynamically?
    primarykey
    data
    text
    <p>I have a new object with a collection of new objects within it on some property as an IList. I see through sql profiler two insert queries being executed.. one for the parent, which has the new guid id, and one for the child, however, the foreign-key on the child that references the parent, is an empty guid. Here is my mapping on the parent: </p> <pre><code>&lt;id name="BackerId"&gt; &lt;generator class="guid" /&gt; &lt;/id&gt; &lt;property name="Name" /&gt; &lt;property name="PostCardSizeId" /&gt; &lt;property name="ItemNumber" /&gt; &lt;bag name="BackerEntries" table="BackerEntry" cascade="all" lazy="false" order-by="Priority"&gt; &lt;key column="BackerId" /&gt; &lt;one-to-many class="BackerEntry" /&gt; &lt;/bag&gt; </code></pre> <p>On the Backer.cs class, I defined BackerEntries property as </p> <pre><code>IList&lt;BackerEntry&gt; </code></pre> <p>When I try to SaveOrUpdate the passed in entity I get the following results in sql profiler:</p> <p>exec sp_executesql N'INSERT INTO Backer (Name, PostCardSizeId, ItemNumber, BackerId) VALUES (@p0, @p1, @p2, @p3)',N'@p0 nvarchar(3),@p1 uniqueidentifier,@p2 nvarchar(3),@p3 uniqueidentifier',@p0=N'qaa',@p1='BC95E7EB-5EE8-44B2-82FF30F5176684D',@p2=N'qaa',@p3='18FBF8CE-FD22-4D08-A3B1-63D6DFF426E5'</p> <p>exec sp_executesql N'INSERT INTO BackerEntry (BackerId, BackerEntryTypeId, Name, Description, MaxLength, IsRequired, Priority, BackerEntryId) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7)',N'@p0 uniqueidentifier,@p1 uniqueidentifier,@p2 nvarchar(5),@p3 nvarchar(5),@p4 int,@p5 bit,@p6 int,@p7 uniqueidentifier',@p0='00000000-0000-0000-0000-000000000000',@p1='2C5BDD33-5DD3-42EC-AA0E-F1E548A5F6E4',@p2=N'qaadf',@p3=N'wasdf',@p4=0,@p5=1,@p6=0,@p7='FE9C4A35-6211-4E17-A75A-60CCB526F1CA'</p> <p>As you can see, its not resetting the empty guid for BackerId on the child to the new real guid of the parent.</p> <p>Finally, the exception throw is: </p> <pre><code>"NHibernate.Exceptions.GenericADOException: could not insert: [CB.ThePostcardCompany.MiddleTier.BackerEntry][SQL: INSERT INTO BackerEntry (BackerId, BackerEntryTypeId, Name, Description, MaxLength, IsRequired, Priority, BackerEntryId) VALUES (?, ?, ?, ?, ?, ?, ?, ?)] ---\u003e System.Data.SqlClient.SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint </code></pre> <p>EDIT: SOLVED! The first answer below pointed me into the correct direction. I needed to add that back reference on the child mapping and class. This allowed it to work in a purely .net way - however, when accepting json, there was a disconnect so I had to come up with some quirky code to 're-attach' the children.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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