简体   繁体   English

外键SaveSaves()上的外键ID和描述-Entity Framework

[英]Foreign key id and description on SaveChanges() overide - Entity Framework

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. 我需要为每个实体添加通用审核日志,因此,我已经覆盖了DbContext类中的SaveChanges()方法并能够创建日志。

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. 我的问题是外键,我需要获取外键的文本值而不是实体的ID。

Thanks in advance 提前致谢

RJ 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 在oldvalue / newvalue变量中,仅返回外键ID,对此我需要描述

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. DataSpace.xxxx还有其他枚举,暴露了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. 这似乎更像是艰巨的挑战。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM