我有以下课程:
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;}
...
}
现在我执行以下操作:
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.
因为上下文已经加载了 Jobs,所以我假设 EF 在我更改 ID 时会更新 _job 的导航属性。然而,这种情况并非如此。在这种情况下,_job.Job 仍然引用与原始 ID (15) 关联的作业。
我对 EF 如何处理导航属性的假设是错误的,还是我只是在这个难题中遗漏了一部分?
谢谢。