Note that there are some explanatory texts on larger screens.

plurals
  1. POfluent nhibernate, unable to resolve property: <property>
    primarykey
    data
    text
    <p>I have a one-to-one relationship in my db and I seem to be having issues with my fluent nhibernate mapping of this relationship. </p> <p>When I attempt to insert a new TableA entity I receive the following error: "unable to resolve property: TableA". The error is thrown on this line in the repository: int id = (int)_session.Save(item);</p> <p><strong>Repository code:</strong></p> <pre><code>public T Save(T item) { try { _session.BeginTransaction(); int id = (int)_session.Save(item); _session.Transaction.Commit(); return _session.Get&lt;T&gt;(id); } catch (Exception) { _session.Transaction.Rollback(); throw; } } </code></pre> <p><strong>Table definitions:</strong></p> <p><strong>Table A</strong></p> <pre><code>Id (int, pk, identity) Field1 (nvarchar) Field2 (date) ... </code></pre> <p><strong>Table B</strong></p> <pre><code>TableAId (int, pk) &lt;-- no fk constraint on these tables Field1 (nvarchar) Field2 (nvarchar) Field3 (bit) </code></pre> <p><strong>Classes:</strong></p> <pre><code>public class TableA { public virtual int Id {get;set;} public virtual string Field1 {get;set;} public virtual DateTime Field2 {get;set;} public virtual TableB TableB {get;set;} } public class TableB { public virtual int TableAId {get;set;} public virtual string Field1 {get;set;} public virtual string Field2 {get;set;} } </code></pre> <p><strong>Mapping:</strong></p> <pre><code>public class TableAMap : ClassMap&lt;TableA&gt; { public TableAMap(){ Id(x =&gt; x.Id); Map(x =&gt; x.Field1); Map(x =&gt; x.Field2); HasOne(x =&gt; x.TableB) .Cascade.SaveUpdate() .Fetch.Join(); } } public class TableBMap : ClassMap&lt;TableB&gt; { public TableBMap() { Id(x =&gt; x.Id, "TableAId").GeneratedBy.Foreign("TableA"); Map(x =&gt; x.Field1); Map(x =&gt; x.Field2); Map(x =&gt; x.Field3); } } </code></pre> <p>I used these as a reference: <a href="http://avinashsing.sunkur.com/2011/09/29/how-to-do-a-one-to-one-mapping-in-fluent-nhibernate/" rel="nofollow noreferrer">http://avinashsing.sunkur.com/2011/09/29/how-to-do-a-one-to-one-mapping-in-fluent-nhibernate/</a></p> <p><a href="https://stackoverflow.com/questions/2966431/one-to-one-mapping-issue-with-nhibernate-fluent-foreign-key-not-updateing">One-to-one Mapping issue with NHibernate/Fluent: Foreign Key not updateing</a></p> <p><a href="https://stackoverflow.com/questions/2298026/indexoutofrangeexception-deep-in-the-bowels-of-nhibernate/2311256#2311256">IndexOutOfRangeException Deep in the bowels of NHibernate</a></p> <p>I've been looking at this for so long now I fear I'm missing something simple (and stupid). </p> <hr> <p><strong>Update:</strong> Tried this:</p> <pre><code>public class TableA { public virtual int Id {get;set;} public virtual string Field1 {get;set;} public virtual DateTime Field2 {get;set;} public virtual TableB TableB {get;set;} public virtual int TableBId { get{return TableB.Id;} set{} } } public class TableAMap : ClassMap&lt;TableA&gt; { public TableAMap() { Id(x =&gt; x.Id); Map(x =&gt; x.Field1); Map(x =&gt; x.Field2); HasOne&lt;TableB&gt;(x =&gt; x.TableB) .Cascade.SaveUpdate() .Fetch.Join() .ForeignKey("TableBId"); } } public class TableBMap : ClassMap&lt;TableB&gt; { public TableBMap() { Id(x =&gt; x.Id, "TableAId"); Map(x =&gt; x.Field1); Map(x =&gt; x.Field2); Map(x =&gt; x.Field3); } } </code></pre> <p>I didn't get the "unable to resolve property: TableA" error this time, but I received a different error. It seems like the Id of the TableA record did not cascade to the TableB record.</p> <hr> <p>Did some more reading.<br> Found this post: <a href="https://stackoverflow.com/questions/1597979/fluent-nhibernate-one-to-one-relationship">fluent NHibernate one-to-one relationship?</a></p> <p>Made some more changes:</p> <pre><code>public class TableAMap : ClassMap&lt;TableA&gt; { public TableAMap() { Id(x =&gt; x.Id); Map(x =&gt; x.Field1); Map(x =&gt; x.Field2); HasOne&lt;TableB&gt;(x =&gt; x.TableB) .Cascade.All() .Constrained() .Fetch.Join() .ForeignKey("TableBId"); } } public class TableBMap : ClassMap&lt;TableB&gt; { public TableBMap() { Id(x =&gt; x.Id, "TableAId").UnsavedValue(0).GeneratedBy.Foreign("TableA"); Map(x =&gt; x.Field1); Map(x =&gt; x.Field2); Map(x =&gt; x.Field3); HasOne&lt;TableA&gt;(x =&gt; x.TableA); } } </code></pre> <p>New error: "attempted to assign id from null one-to-one property: TableA"</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