Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A few problems with your mapping... You have a bidirectional relationship and NHibernate needs to know which way to update it. In the OO world, references only go one way and there is no way for NHibernate to know that Parent->Children is the same FK as Child->Parent. Right now you have Child->Parent set to ReadOnly(). This is telling NHibernate not to update this property. So it tries to insert the Child (with a null parent) and then update the FK from the Parent side. This isn't working if you have a not null constraint on your FK. The usual way to map this is to use Inverse=true on the parent side and let the child worry about persistence. (It's your job in the OO model to ensure that the Parent->Children collection contains the same set of references as the set of Child->Parent relationships.)</p> <pre><code>public class ParentMap : ClassMap&lt;Parent&gt; { public ParentMap() { Id(x =&gt; x.ParentId).GeneratedBy.Native(); Map(x =&gt; x.Name); HasMany(x =&gt; x.Children).KeyColumn("ParentId").Inverse().Cascade.SaveUpdate(); } } public class ChildMap : ClassMap&lt;Child&gt; { public ChildMap() { Id(x =&gt; x.ChildId).GeneratedBy.Native(); Map(x =&gt; x.Name); References(x =&gt; x.Parent).Column("ParentId").Not.Nullable(); // Removed ReadOnly() } } </code></pre> <p>The SQL statements sent to the database when saving are now:</p> <pre><code>INSERT INTO [Parent] (Name) VALUES ('P1' /* @p0 */) select SCOPE_IDENTITY() INSERT INTO [Child] (Name, ParentId) VALUES ('C1' /* @p0 */, 1 /* @p1 */) select SCOPE_IDENTITY() </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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