I have a requirement to add a generic audit log for each entity, therefore I have overridden the SaveChanges()
method in the DbContext
class and able to create the logs.
My issue is with the foreign keys, I need to get the text value of the foreign key instead of the id for the entities.
Thanks in advance
RJ
here is the code
if (dbEntry.State == System.Data.EntityState.Modified)
{
if (!object.Equals(dbEntry.GetDatabaseValues().GetValue<object>(propertyName), dbEntry.CurrentValues.GetValue<object>(propertyName)))
{
var oldvalue = dbEntry.GetDatabaseValues().GetValue<object>(propertyName) != null ? dbEntry.GetDatabaseValues().GetValue<object>(propertyName).ToString() : "";
var newvalue = dbEntry.CurrentValues.GetValue<object>(propertyName) != null ? dbEntry.CurrentValues.GetValue<object>(propertyName).ToString() : "";
Auidlog += displayName + ": Changed from - " + oldvalue + " To - " + newvalue + " ";
}
}
in the oldvalue/newvalue variable return only the foreign key id, I need the description for that
You can access the metadata of the model using:
ObjectContext objContext = ((IObjectContextAdapter)context).ObjectContext;
MetadataWorkspace workspace = objContext.MetadataWorkspace;
IEnumerable<EntityType> managedTypes = workspace.GetItems<EntityType>(DataSpace.OSpace);
The DataSpace.xxxx has other enums exposing different views of the metaData. So check them all out.
But how do you plan to access the "text" field of the Primary table. Find the a string ? find a property that contains the term Name or text ? Perhaps you can implement an interface on EVERY primary table. Or add an attribute to the main text field of the table.
That seems more like the outstanding challenge.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.