I work on my solution based on ASP.NET MVC with EF Code First.
I have the following model classes:
public class Request
{
[Key]
public virtual int RequestID { get; set; }
public virtual string BudgetNumber { get; set; }
public virtual string SiteCustom { get; set; }
public virtual Vehicle Vehicle { get; set; }
}
public class Vehicle
{
public int VehicleID { get; set; }
public string DesignationFr { get; set; }
public string DesignationNl { get; set; }
public string DesignationEn { get; set; }
}
I need to log user modifications. For example, if user changed the SiteCustom
value of the Request, I can easily track it with the code below. So inside the SaveChanges I intercept every added & modified entries.
public override int SaveChanges()
{
ChangeTracker.DetectChanges(); // Important!
System.Data.Objects.ObjectContext ctx = ((IObjectContextAdapter)this).ObjectContext;
List<ObjectStateEntry> objectStateEntryList = ctx.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Modified).ToList();
foreach (ObjectStateEntry entry in objectStateEntryList)
{
if (entry.IsRelationship)
{
}
if (!entry.IsRelationship)
{
if (entry.Entity is Request)
{
var request = (Request)entry.Entity;
...
}
}
}
return base.SaveChanges();
}
As you can see in the code above, when the entry is not a relationship, I can easily retrieve the entity behind it.
My problem: when the user modified the Vehicle
property of the request this is a relationship and I didn't succeed to retrieve the entities when this is a relationship. I mean not only retrieve the key values of the 2 parts of the relationship but all the fields of the entities. Is it possible?