I have a User table with a corresponding UserAudits table that tracks the changes. The operation type is stored in the audit table (insert, update, delete).
I want to create a navigation property on my User entity so I can fetch the "Inserted" record from the audit table. Is this possible? Usually when setting up a navigation property there is no way to specify extra conditions on the join.
User:
public class User
{
public int Id {get; set;}
public string Name {get;set;}
public UserAudit InsertedAuditRecord {get; set;} // <-- How to configure this?
}
UserAudit:
public class UserAudit
{
public int AuditId {get; set;
public int Id {get; set;}
public string Name {get; set;}
public string OperationType {get; set;}
}
I know I could create a one-to-many navigation property on my User entity and then query that:
ICollection<UserAudit> UserAudits {get; set;}
UserAudit InsertedAuditRecord => UserAudits.First(ua => ua.OperationType = "insert");
But this would require bringing the entire audit table for the user into memory just to find a single record.
Surely there's some way to accomplish this?