I am using the EntityFramework.Extended library to implement audit logging in my system. However, when I update the foreign key 'TitleId' on my Person object, it does not register it as a foreign key and does not replace the Id with my chosen display field.
Audit log config: (this is called in my Global.asax startup file)
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);
}
}
Person entity:
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; }
}
Title entity:
public class Title
{
public int Id { get; set; }
public string Abbreviation { get; set; }
public string Description { get; set; }
}
Update logic:
var audit = BeginAudit();
person.TitleId = model.TitleId;
SaveChanges();
var log = audit.LastLog;
When lookign through the property changes in audit.LastLog
, the TitleId modification is not recognized as a relationship and is not applying my display conversion specified in the config.
The attribute for foreign key should go on the navigation property, not the foreign key itself:
[ForeignKey("TitleId")]
public virtual Title Title { get; set; }
But in this case you don't need the attribute at all, because your class is following a naming convention that Entity Framework recognises
And, since Title
isn't the foreign key - TitleId
is, I'd try this:
// Use abbreviation value in audit log whenever TitleId is used as foreign key
auditConfiguration.IsAuditable<TitleId>()
.DisplayMember(x => x.Abbreviation);
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.