Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>How to get the database column / field names and class property names for an entity mapped by NHibernate:</strong></p> <pre><code>using System; using System.Collections.Generic; using System.Reflection; using NHibernate; using NHibernate.Persister.Entity; namespace Stackoverflow.Example { /// &lt;summary&gt; /// NHibernate helper class /// &lt;/summary&gt; /// &lt;remarks&gt; /// Assumes you are using NHibernate version 3.1.0.4000 or greater (Not tested on previous versions) /// &lt;/remarks&gt; public class NHibernateHelper { /// &lt;summary&gt; /// Creates a dictionary of property and database column/field name given an /// NHibernate mapped entity /// &lt;/summary&gt; /// &lt;remarks&gt; /// This method uses reflection to obtain an NHibernate internal private dictionary. /// This is the easiest method I know that will also work with entitys that have mapped components. /// &lt;/remarks&gt; /// &lt;param name="sessionFactory"&gt;NHibernate SessionFactory&lt;/param&gt; /// &lt;param name="entity"&gt;An mapped entity&lt;/param&gt; /// &lt;returns&gt;Entity Property/Database column dictionary&lt;/returns&gt; public static Dictionary&lt;string, string&gt; GetPropertyAndColumnNames(ISessionFactory sessionFactory, object entity) { // Get the objects type Type type = entity.GetType(); // Get the entity's NHibernate metadata var metaData = sessionFactory.GetClassMetadata(type.ToString()); // Gets the entity's persister var persister = (AbstractEntityPersister)metaData; // Creating our own Dictionary&lt;Entity property name, Database column/filed name&gt;() var d = new Dictionary&lt;string, string&gt;(); // Get the entity's identifier string entityIdentifier = metaData.IdentifierPropertyName; // Get the database identifier // Note: We are only getting the first key column. // Adjust this code to your needs if you are using composite keys! string databaseIdentifier = persister.KeyColumnNames[0]; // Adding the identifier as the first entry d.Add(entityIdentifier, databaseIdentifier); // Using reflection to get a private field on the AbstractEntityPersister class var fieldInfo = typeof(AbstractEntityPersister) .GetField("subclassPropertyColumnNames", BindingFlags.NonPublic | BindingFlags.Instance); // This internal NHibernate dictionary contains the entity property name as a key and // database column/field name as the value var pairs = (Dictionary&lt;string, string[]&gt;)fieldInfo.GetValue(persister); foreach (var pair in pairs) { if (pair.Value.Length &gt; 0) { // The database identifier typically appears more than once in the NHibernate dictionary // so we are just filtering it out since we have already added it to our own dictionary if (pair.Value[0] == databaseIdentifier) break; d.Add(pair.Key, pair.Value[0]); } } return d; } } } </code></pre> <p>Usage:</p> <pre><code>// Get your NHiberate SessionFactory wherever that is in your application var sessionFactory = NHibernateHelper.SessionFactory; // Get an entity that you know is mapped by NHibernate var customer = new Customer(); // Get a dictionary of the database column / field names and their corresponding entity property names var propertyAndColumnNamesDictionary = Stackoverflow.Example.NHibernateHelper.GetPropertyAndColumnNames(sessionFactory, customer); </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