Note that there are some explanatory texts on larger screens.

plurals
  1. POFluent Nhibernate fails during insert
    text
    copied!<p>I have problem using Fluent Nhibernate, I have following model. When I try to save Hotel with has new Geography I getting foreign key exception, looks like Nhibenate fails to save data in correct order, is it something I can correct via Fluent Nhibernate ?</p> <pre><code>public class Geography { public virtual int CityID { get; set; } public virtual string CountryCode { get; set; } } public class Hotel { public virtual int HotelID { get; set; } public virtual Geography City { get; set; } } </code></pre> <p>Mapping</p> <pre><code> public class HotelMap : ClassMap&lt;Hotel&gt; { public HotelMap() { Id(x =&gt; x.HotelID) .GeneratedBy .Identity(); References(x =&gt; x.City, "CityId") .Cascade.All(); } } public class GeographyMap : ClassMap&lt;Geography&gt; { public GeographyMap() { Id(x =&gt; x.CityID); Map(x =&gt; x.CountryCode); HasMany(a =&gt; a.Hotels) .Cascade.All(); } } </code></pre> <p>Added generated mappings</p> <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true"&gt; &lt;class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Hotel" table="`Hotel`"&gt; &lt;id name="HotelID" type="System.Int32"&gt; &lt;column name="HotelID" /&gt; &lt;generator class="assigned" /&gt; &lt;/id&gt; &lt;many-to-one cascade="all" class="Geography" foreign-key="HotelGeography" name="City"&gt; &lt;column name="CityId" /&gt; &lt;/many-to-one&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="property" auto-import="true" default-cascade="none" default-lazy="true"&gt; &lt;class xmlns="urn:nhibernate-mapping-2.2" mutable="true" name="Geography" table="`Geography`"&gt; &lt;id name="CityID" type="System.Int32"&gt; &lt;column name="CityID" /&gt; &lt;generator class="assigned" /&gt; &lt;/id&gt; &lt;bag cascade="all" inverse="true" name="Hotels" mutable="true"&gt; &lt;key&gt; &lt;column name="HotelID" /&gt; &lt;/key&gt; &lt;one-to-many class="Hotel" /&gt; &lt;/bag&gt; &lt;property name="CountryCode" type="System.String"&gt; &lt;column name="CountryCode" /&gt; &lt;/property&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre>
 

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