Create a view that uses a case statement to return the table name and id for the foreign key column in use:
Code:
select id,
case
when ColA is not null then 'TableA'
when ColB is not null then 'TableB'
-- etc.
end as TableName,
case
when ColA is not null then ColA
when ColB is not null then ColB
-- etc.
end as EntityId
from Events
Map that to your Event class. Create a method on your event class to load the IEventRecordable:
Code:
public virtual IEventRecordable GetEventRecordable()
{
switch (TableName)
{
case "TableA":
session.Get(typeof(EntityA), EntityId);
break;
case "TableB":
session.Get(typeof(EntityB), EntityId);
break;
// etc.
}
}
It's ugly but any solution will be ugly due to the structure of your events table. If it's a new application, I would structure your Events table to store TableName, ForeignKey instead of N columns. I wouldn't worry about enforcing foreign keys on an audit table.
Hope this helps.