Note that there are some explanatory texts on larger screens.

plurals
  1. POOne-to-one Mapping issue with NHibernate/Fluent: Foreign Key not updateing
    primarykey
    data
    text
    <p>Summary: Parent and Child class. One to one relationship between the Parent and Child. Parent has a FK property which references the primary key of the Child. Code as follows:</p> <pre><code> public class NHTestParent { public virtual Guid NHTestParentId { get; set; } public virtual Guid ChildId { get { return ChildRef.NHTestChildId; } set { } } public virtual string ParentName { get; set; } protected NHTestChild _childRef; public virtual NHTestChild ChildRef { get { if (_childRef == null) _childRef = new NHTestChild(); return _childRef; } set { _childRef = value; } } } public class NHTestChild { public virtual Guid NHTestChildId { get; set; } public virtual string ChildName { get; set; } } </code></pre> <p>With the following Fluent mappings:</p> <p>Parent Mapping</p> <pre><code> Id(x =&gt; x.NHTestParentId); Map(x =&gt; x.ParentName); Map(x =&gt; x.ChildId); References(x =&gt; x.ChildRef, "ChildId").Cascade.All(); </code></pre> <p>Child Mapping:</p> <pre><code> Id(x =&gt; x.NHTestChildId); Map(x =&gt; x.ChildName); </code></pre> <p>If I do something like (pseudo code) ...</p> <pre><code>HTestParent parent = new NHTestParent(); parent.ParentName = "Parent 1"; parent.ChildRef.ChildName = "Child 1"; nhibernateSession.SaveOrUpdate(aParent); Commit; </code></pre> <p>... I get an error: "Invalid index 3 for this SqlParameterCollection with Count=3"</p> <p>If I change the parent 'References' line as follows (i.e. provide the name of the child property I'm pointing at):</p> <pre><code>References(x =&gt; x.ChildRef, "ChildId").PropertyRef("NHTestChildId").Cascade.All(); </code></pre> <p>I get the error: "Unable to resolve property: NHTestChildId" So, I tried the 'HasOne()' reference setting, as follows:</p> <pre><code>HasOne&lt;NHTestChild&gt;(x =&gt; x.ChildRef).ForeignKey("ChildId").Cascade.All().Fetch.Join(); </code></pre> <p>In this arrangement the save works (and data in db is as wanted), but loading fails to find the child entity. Inspecting the SQL Nhibernate produces shows me that NHibernate is assuming the Primary key of the parent is the link to the child (i.e. load join condition is "parent.NHTestParentId = child.NHTestChildId). The 'ForeignKey' I specified appears to be ignored - if fact I can set any value (even a non-existance field) and no error occurs - the join just always fails and no child is returned.</p> <p>I've tried a number of slight variations on the above. It seems like it should be a simple thing to achieve. Any ideas?</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