Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting class field names and table column names from NHibernate metadata
    primarykey
    data
    text
    <p><strong>Background</strong></p> <p>I am using a legacy database with all kinds of ugly corners. One bit is auditing. There is a table listing tablename/field combinations of fields that should have an audit trail. For example, if there is a row that has "WORKORDER" for the table name and "STATUS" for the fieldname, then I need to add row(s) to the auditing table whenever the Workorder.Status property changes in the application. I know the approach: NH events or interceptors, but I've got an issue to figure out before I get to that stage.</p> <p><strong>Question</strong></p> <p>What I need to know is how to get a list of key/value pairs for a single persistent class containing (a) the database field name and (b) the associated property name in the class. So for my example, I have a class called Workorder associated with a table called (no surprise) WORKORDER. I have a property on that Workorder class called CurrentStatus. The matching property in the WORKORDER table is STATUS. Notice the mismatch between the property name and table column name? I need to know the property name to access the before and after data for the audit. But I also need to know the backing column name so that I can query the stupid legacy "AuditTheseColumns" table.</p> <p><strong>What I've tried</strong></p> <p>in my application I change the Workorder.CurrentStatus from "TS" to "IP". I look in my audit tracking table and see that the WORKORDER.STATUS column is tracked. So after calling Session.SaveOrUpdate(workorder), I need to find the Workorder property associated with the STATUS column and do a Session.Save(auditRecord) telling it the old ("TS") and new ("IP") values.</p> <p>As far as I can tell, you can get information about the class:</p> <pre><code> var fieldNames = new List&lt;string&gt;(); IClassMetadata classMetadata = SessionFactory(Resources.CityworksDatasource).GetClassMetadata(typeof(T)); int propertyCount = 0; foreach (IType propertyType in classMetadata.PropertyTypes) { if (propertyType.IsComponentType) { var cp = (ComponentType)propertyType; foreach (string propertyName in cp.PropertyNames) { fieldNames.Add(propertyName); } } else if(!propertyType.IsCollectionType) { fieldNames.Add(classMetadata.PropertyNames[propertyCount + 1]); } propertyCount++; } </code></pre> <p>And information about the table:</p> <pre><code> var columnNames = new List&lt;string&gt;(); PersistentClass mappingMeta = ConfigureCityworks().GetClassMapping(typeof(T)); foreach (Property property in mappingMeta.PropertyIterator) { foreach (Column selectable in property.ColumnIterator) { if (columnNames.Contains(selectable.Name)) continue; columnNames.Add(selectable.Name); } } </code></pre> <p>But not at the same time. Any ideas? I'm at a loss where to look next.</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.
 

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