我正在使用EntityFramework.Extended库在我的系统中实现审计日志记录。但是,当我在Person对象上更新外键'TitleId'时,它不会将其注册为外键,也不会将Id替换为我选择的显示字段。
审核日志配置(这在我的Global.asax启动文件中调用)
public static class AuditLogConfiguration
{
public static void Register()
{
var auditConfiguration = AuditConfiguration.Default;
auditConfiguration.IncludeRelationships = true;
auditConfiguration.LoadRelationships = true;
auditConfiguration.DefaultAuditable = true;
// Use abbreviation value in audit log whenever Title is used as foreign key
auditConfiguration.IsAuditable<Title>()
.DisplayMember(x => x.Abbreviation);
}
}
人员实体:
public class Person
{
[Key]
public int Id { get; set; }
[ForeignKey("Title")]
public int TitleId { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public virtual Title Title { get; set; }
}
标题实体:
public class Title
{
public int Id { get; set; }
public string Abbreviation { get; set; }
public string Description { get; set; }
}
更新逻辑:
var audit = BeginAudit();
person.TitleId = model.TitleId;
SaveChanges();
var log = audit.LastLog;
当查看audit.LastLog
中的属性更改时,TitleId修改不会被识别为关系,并且不会应用我在配置中指定的显示转换。
答案 0 :(得分:0)
外键的属性应该放在导航属性上,而不是外键本身:
[ForeignKey("TitleId")]
public virtual Title Title { get; set; }
但在这种情况下,您根本不需要该属性,因为您的类遵循实体框架识别的命名约定
而且,由于Title
不是外键 - TitleId
,我试试这个:
// Use abbreviation value in audit log whenever TitleId is used as foreign key
auditConfiguration.IsAuditable<TitleId>()
.DisplayMember(x => x.Abbreviation);