Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find unmapped properties in a NHibernate mapped class?
    text
    copied!<p>I just had a NHibernate related problem where I forgot to map one property of a class.</p> <p>A very simplified example:</p> <pre><code>public class MyClass { public virtual int ID { get; set; } public virtual string SomeText { get; set; } public virtual int SomeNumber { get; set; } } </code></pre> <p>...and the mapping file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyAssembly" namespace="MyAssembly.MyNamespace"&gt; &lt;class name="MyClass" table="SomeTable"&gt; &lt;property name="ID" /&gt; &lt;property name="SomeText" /&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>In this simple example, you can see the problem at once:<br> there is a property named "SomeNumber" in the class, but not in the mapping file.<br> So NHibernate will not map it and it will always be zero.</p> <p>The real class had a lot more properties, so the problem was not as easy to see and it took me quite some time to figure out why SomeNumber always returned zero even though I was 100% sure that the value in the database was != zero.</p> <p><strong>So, here is my question:</strong> </p> <p>Is there some simple way to find this out via NHibernate?<br> Like a compiler warning when a class is mapped, but some of its properties are not.<br> Or some query that I can run that shows me unmapped properties in mapped classes...you get the idea.</p> <p>(Plus, it would be nice if I could exclude some legacy columns that I <em>really</em> don't want mapped.)</p> <p><strong>EDIT:</strong><br> Okay, I looked at everything you proposed and decided to go with the meta-data API...that looks the easiest to understand for me.<br> Now that I know what to search for, I found some examples which helped me to get started.<br> So far, I have this:</p> <pre><code>Type type = typeof(MyClass); IClassMetadata meta = MySessionFactory.GetClassMetadata(type); PropertyInfo[] infos = type.GetProperties(); foreach (PropertyInfo info in infos) { if (meta.PropertyNames.Contains(info.Name)) { Console.WriteLine("{0} is mapped!", info.Name); } else { Console.WriteLine("{0} is not mapped!", info.Name); } } </code></pre> <p>It nearly works, except one thing: IClassMetadata.PropertyNames returns the names of all the properties <strong>except</strong> the ID.<br> To get the ID, I have to use IClassMetadata.IdentifierPropertyName. </p> <p>Yes, I could save .PropertyNames in a new array, add .IdentifierPropertyName to it and search <em>that</em> array.<br> But this looks strange to me.<br> Is there no better way to get <strong>all</strong> mapped properties including the ID?</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