我正在尝试将更改记录到数据库,以便用户可以查看谁更改了哪些内容。我正在使用DbEntityEntry
来检查并记录已修改的DbPropertyEntity
。当我想要将更改记录到导航属性时,我遇到了问题。我使用Reference()
方法来获得对导航属性的引用,但不像DbPropertyEntity
,DbReferenceEntry
没有OriginalValue
只有CurrentValue
属性。你如何获得导航属性的OriginalValue
?DbPropertyEntry获取原始值
//Get the field that hold the id of the foreign key
var field = entry.Property(x => x.field);
//Check to see if the user changed the value
if (field.IsModified)
{
//Get the reference property associated with the field
var fieldRef = entry.Reference(x => x.fieldRef);
//Log the id change
Log(field.Name, field.CurrentValue, field.OriginalValue);
//Can't get the OriginalValue
Log(fieldRef.Name, fieldRef.CurrentValue, ???);
}
我没有使用edmx工具,我只是创建了一个数据库并添加了字段,然后更新了我的域对象。我正在记录外键更改。问题是用户会看到这个日志键的变化没有意义(例如TypeID从3改为4),我需要在导航属性中记录更改(例如Type从Student值更改为老师)。 – 2012-07-11 20:56:10