Suppose I have a database table User with a 1:1 relationship with table Role. The class generated by Entity Framework would be:
// Generated by Entity Framework
public partial class User
{
public int ID { get; set; }
public string Name { get; set; }
public Guid RoleID { get; set; }
public Role Role { get; set; }
}
Now for auditing purposes I want to save the Role name, so I define my own partial class with my own property:
// Written by myself
public partial class User
{
public string RoleName {get { Role.Name; }}
}
How would I tell EntityFramework to track User.RoleName just like it tracks User.Name?
var modifiedEntities = ChangeTracker.Entries();
foreach (var change in modifiedEntities)
{
var name = change.Property("Name").OriginalValue; // ok
var roleName = change.Property("RoleName").OriginalValue; // gives error
}
The error says "Member 'OriginalValue' cannot be called for property 'RoleName' on entity of type 'User' because the property is not part of the Entity Data Model."