I've got a PostUpdateEvent logging all dirty fields. I use the PostUpdateEvent.Persister.ClassMetaData (from the persister) to weed out collections.
However, when I get to a many-to-one relationship, I need to log the value referenced in the foreign key, not the object itself.
So I could have the following mapping:
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false" assembly="McDo.Core" default-lazy="true">
<class name="WorkerInfo, McDo.Core" table="WorkerInfo">
<id name="Id" column="WIN_ID" type="Int64">
<generator class="native" />
</id>
<version name="Version" column="WIN_Version" />
<property name="WeeklyHours" column="WIN_WeeklyHours" />
<many-to-one name="WorkSchedule" column="WIN_WRS_ID" not-null="false" cascade="none" />
</class>
</hibernate-mapping>
Let's say I have a worker that has workschedule with an ID of 5. I then go in and select a different workschedule (ID = 10) for him.
If I try to get the value like this:
Code:
var oldVal = postUpdateEvent.OldState[i].ToString();
var newVal = postUpdateEvent.State[i].ToString();
it'll return "McDo.Core.WorkSchedule", instead of "10".
Now, I know I can check for EntityTypes doing this:
Code:
var cmd = postUpdateEvent.Persister.ClassMetadata;
var isEntity = cmd.PropertyTypes[i].IsEntityType;
but once I've discovered it's an entity, how would I go about getting the value of the id in the foreign key?