Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate ComponentAsId() does not actually set Id property
    primarykey
    data
    text
    <p>I am trying to set up NHibernate (3.2.0.4000) using the coded mapping but I can't get the ComponentAsId working properly.</p> <p>I have a database schema that I can't change where the primary key consists of a string and a date so I need to use a component as the Id:</p> <p>i.e.</p> <h2>Database schema</h2> <pre><code>Id varchar(20) NOT NULL, EffectiveDate datetime NOT NULL, Property1 int </code></pre> <h2>Classes</h2> <pre><code>public class EffectiveDateId { public string Id { get; set; } public DateTimeOffset EffectiveDate { get; set; } } public class MyClass { public EffectiveDateId Identity { get; set; } public int Property1 { get; set; } } </code></pre> <h2>Mapping</h2> <pre><code>var mapper = new ConventionModelMapper(); mapper.Class&lt;MyClass&gt;(map =&gt; map.ComponentAsId(id =&gt; id.Identity, cid =&gt; { cid.Property(x =&gt; x.Id, x =&gt; x.Length(20)); cid.Property(x =&gt; x.EffectiveDate); })); </code></pre> <p>But this does not work as the Identity propery on MyClass is null.</p> <p>The mapping as XML looks like this:</p> <pre><code>&lt;hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:nhibernate-mapping-2.2"&gt; &lt;class name="Test.MyClass, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"&gt; &lt;composite-id class="EffectiveDateId, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"&gt; &lt;key-property name="Id" type="AnsiString" length="20" /&gt; &lt;key-property name="EffectiveDate" /&gt; &lt;/composite-id&gt; &lt;property name="Property1" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>However if I manually change the XML to include <strong>name="Identity"</strong> like this:</p> <pre><code>&lt;hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="urn:nhibernate-mapping-2.2"&gt; &lt;class name="Test.MyClass, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"&gt; &lt;composite-id name="Identity" class="EffectiveDateId, Test, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"&gt; &lt;key-property name="Id" type="AnsiString" length="20" /&gt; &lt;key-property name="EffectiveDate" /&gt; &lt;/composite-id&gt; &lt;property name="Property1" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>it works.</p> <p>I've tried setting the id by adding (in addition to the ComponentAsId mapping)</p> <pre><code>mapper.Class&lt;MyClass&gt;(map =&gt; map.Id(id =&gt; id.Identity)); </code></pre> <p>but that just generates an NHibernate.MappingException</p> <blockquote> <p>Ambiguous mapping of Test.MyClass id. An id property, with generator, was defined and you are trying to map the component Test.EffectiveDateId, of property 'Identity', as id for Test.MyClass.</p> </blockquote> <p>Using the debugger I can see that for the code mapped configuration, the ClassMapping.IdentifierProperty is null while in the XML confguration it is set to a NHibernate.Mapping.Property instance.</p> <p>So, is there a bug in the implementation of ComponentAsId() that is not setting the IdentifierProperty or am I doing it wrong?</p> <p>regards, Eamon</p>
    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. 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