private int getTableKey(IList primaryKeyFields)
{
#region Get Table Key
if (primaryKeyFields.Count == 0)
{
return -1;
}
//in inheritance situations, there are multiple keys, one from each table in the inheritance
//all have the same value though!
IEntityField keyfield = (IEntityField)primaryKeyFields[0];
if (keyfield.ActualDotNetType != typeof(int) || keyfield.DbValue == null)
return -1;
return (int)keyfield.DbValue;
#endregion
}
Don't use the DBValue for this method. DBValue is not populated at this point because technically the entity isn't refetched yet, and the whole save process isn't finish yet. Remember that if you raise an exception at this point, the transaction will be rollback, so you can't refer DBValue yet.
In short, use CurrentValue instead, this will work for new and existing entities:
private int getTableKey(IList primaryKeyFields)
{
#region Get Table Key
if (primaryKeyFields.Count == 0)
{
return -1;
}
//in inheritance situations, there are multiple keys, one from each table in the inheritance
//all have the same value though!
IEntityField keyfield = (IEntityField)primaryKeyFields[0];
if (keyfield.ActualDotNetType != typeof(int) || keyfield.CurrentValue == null)
return -1;
return (int)keyfield.CurrentValue;
#endregion
}