I have the following classes:
public class Person {
public int Id {get; set;}
...
public ICollection<PersonalJobs> Jobs {get; set;}
}
public class PersonalJob {
public int Id {get; set;}
public int PersonId {get; set;}
public int JobId {get; set;}
public Job Job {get; set;}
public DateTime StartDate {get; set;}
...
}
public class Job {
public int Id {get; set;}
public string Description {get; set;}
...
}
Now I do something like the following:
var _personId = 1;
var _jobId = 15;
_context.Set<Jobs>().Load(); //Pre-Load all jobs
var _person = _context.Persons.Include(p => p.Jobs.Select(j => j.Job))
.SingleOrDefault(p => p.Id == _personId);
var _job = _person.Jobs.SingleOrDefault(p => p.JobId == _jobId);
_job.JobId = 23; //Change the job that this person is tied to.
// At this point, if I try to access _job.Job.Description, it still references the old job.
Because the context already had Jobs loaded, I assumed that EF would update the navigation property of _job when I change the ID. However, this is not the case. In this case _job.Job still references the job associated with the original Id (15).
Am I wrong in my assumption of how EF handles navigation properties or am I just missing a piece in this puzzle?
Thanks.
You need to call
SaveChanges()to persist to the database:This only works because Job 23 is already loaded into your
DbContext.If an object is loaded, EF will hook up your property when you call
SaveChanges()EDIT:
You’ve loaded all the Jobs, so you could just set the property yourself:
This won’t result in any database calls – it will be satisfied from your
DbContextIt will result in the same
UPDATEstatement when you do callSaveChanges()