I try to use ChangeTracker
from EF to do audit and save all changes in database (add + update).
So with ChangeTracker
I will able to get all changed tables, and all values for each. But I also need of the ID of new records in new tables.
Some of them are relationship, and when I try to get the values with EntityKey
, the primary key is 0
private object GetPrimaryKeyValue(DbEntityEntry entry)
{
var objectStateEntry = ((IObjectContextAdapter)this).ObjectContext.ObjectStateManager.GetObjectStateEntry(entry.Entity);
return objectStateEntry.EntityKey.EntityKeyValues[0].Value;
}
Someone knows, if I'm on the wrong way and if it's possible to get the primary key of related entity ?
Thanks !