Checking original value of navigation property in Entity Framework 6

Viewed 107

In my application the DbContext is persisted and before saving I want to check what changes the user has made and log these. This may not be the most elegant or efficient method of audit tracking the application, but I have gone a long way down this path.

For ordinary properties I can use db.Entry(item).OriginalValues. For navigation properties, where the navigation property has already been loaded and I don't want to overwrite the value that the user has set, how do I check the original navigation property, do I have to implement the foreign key as an int property? Or is there some way to get the property, or its Key, from the existing context?

public class User
{
    public string Name { get; set;}  

    public DateTime DateOfBirth { get; set; }
}

public class Job
{
    public string Description { get; set;}

    public User Asignee {get; set;}
}

In the above code the job has been retrieved from the database using EF and the Asignee relationship property has been loaded. Then the operator changes the Asignee to a different User. How can I check which entity (if any) the navigation property previously navigated to?

0 Answers
Related