ChangeTracker doesn't show navigation property changes in Entity Framework

737 views Asked by At

I have the below structure in Entity framework

[Table("Person")]
public class Person
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public long Id{get  ;set;}
    public string Name{get;set;}
    public long DId { get; set; } // this is id from Department Table

    [ForeignKey("DId")]
    public Department SudentDepartment{get;set;}
}
[Table("Department")]
public class Department
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public long Id{get  ;set;}
    public string Name { get; set; }
}

Now, I am overriding the SaveChanges method to Audit changes in Audit Table at field level. I am unable to get old and new value of Department in ChangeTracker as it is navigation property. This is required because in Audit table I need to save Departname instead of Id.

Is there any way I could get the navigation property in ChangeTracker?

1

There are 1 answers

0
thepirat000 On BEST ANSWER

Only input I have is that you could try to use a code similar to the one on Audit.EntityFramework library.

Check the GetForeignKeysValues code here: https://github.com/thepirat000/Audit.NET/blob/master/src/Audit.EntityFramework/EntityKeyHelper.cs#L145