Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing an IUsertype with an ID field in NHibernate 3.3+ Mapping-By-Code
    text
    copied!<p>I am working with <strong>NHibernate 3.3</strong> and using the <strong>mapping-by-code</strong> system. The table/database I am working with is going to <strong>read-only</strong> for my application. </p> <p>The problem I am facing is that my primary key column is stored as a binary field in SQL Server. I need to read this as a string, and unfortunately I cannot modify the table (including adding indexed views).</p> <p>At this moment, I am trying to use an <strong>IUsertype</strong> to convert the value from binary to string. However, I am stuck when trying to set the type of an Id column in an entity to use an IUserType.</p> <p>I have managed to successfully do it for normal properties as the below example, but can't figure out how to do it for ID columns and foreign key columns.</p> <pre><code>public class ExampleEntity { public virtual String MyIdColumn { get; set; } public virtual Country Country { get; set; } } public class ExampleEntityMap : ClassMapping&lt;ExampleEntity&gt; { public ExampleEntityMap() { Table("Table"); Id(i =&gt; i.Id, map =&gt; { map.Column("MyIdColumn"); map.Type(???); }); Property(i =&gt; i.Country, map =&gt; { map.Column("Country"); map.Type&lt;CountryEnumUserType&gt;(); }); } } </code></pre> <ol> <li>Is this possible with NH3.3 Mapping By Code? </li> <li>Must I implement an <strong>IIdentifierType</strong> instead to achieve what an IUserType does for an Id field?</li> <li>Can a NHibernate transformer achieve what I am doing?</li> <li>Is there another way to solve this? Other than retrieving the data and converting it in C#, as I have to do this for many columns in over a dozen tables.</li> </ol> <p>Thanks</p>
 

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