Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>How to get the database column names for an entity mapped by NHibernate:</strong></p> <pre><code>using System; using System.Collections.Generic; 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; /// Gets the list of database column names for an entity /// &lt;/summary&gt; /// &lt;param name="sessionFactory"&gt;NHibernate SessionFactory&lt;/param&gt; /// &lt;param name="entity"&gt;A mapped entity&lt;/param&gt; /// &lt;returns&gt;List of column names&lt;/returns&gt; public static IEnumerable&lt;string&gt; GetPropertyColumnNames(ISessionFactory sessionFactory, object entity) { Type entityType = entity == null ? null : entity.GetType(); List&lt;string&gt; columnNameList = null; // This has some cool methods and properties so check it out var metaData = entityType == null ? null : sessionFactory.GetClassMetadata(entityType.ToString()); //- metaData validity check ... will be null if provided type is not mapped if (metaData != null) { // This has some even cooler methods and properties so definitely check this out var entityPersister = (AbstractEntityPersister) metaData; //- how to get the entity's identifier //- string entityIdentifier = metaData.IdentifierPropertyName; //- Get the database identifier //- can have multiple in case of composite keys IEnumerable&lt;string&gt; dbIdentifierNameList = entityPersister.KeyColumnNames; var propertyNameList = entityPersister.PropertyNames; // Adding the database identifier first columnNameList = new List&lt;string&gt;(dbIdentifierNameList); //- then add properties column names foreach (var propertyName in propertyNameList) { var columnNameArray = entityPersister.GetPropertyColumnNames(propertyName); columnNameList.AddRange(columnNameArray.Where(columnName =&gt; dbIdentifierNameList.Contains(columnName) == false)); } } return columnNameList; } } } </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 list of the database column names for the entity var columnNames = Stackoverflow.Example.NHibernateHelper.GetPropertyColumnNames( sessionFactory, customer ); </code></pre> <p>Bask in the glory of this awesomeness :)</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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