Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8976107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:02:55+00:00 2026-06-15T19:02:55+00:00

I have the following classes: public class Person { public int Id {get; set;}

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:02:56+00:00Added an answer on June 15, 2026 at 7:02 pm

    You need to call SaveChanges() to persist to the database:

    _job.JobId = 23;
    
    _context.SaveChanges();
    
    // _job.Job now points to Job 23
    

    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:

    _job.Job = _context.Set<Jobs>().Find( 23 );
    

    This won’t result in any database calls – it will be satisfied from your DbContext

    It will result in the same UPDATE statement when you do call SaveChanges()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following classes: public class Order { public int Oid {get; set;}
I have following classes public class Person { public string FirstName { get; set;
I have the following classes: class Person { public string Name { get; set;
I have the following classes: public class Menu { public int Order { get;
I have 3 following classes: public class BaseProperty1{ public string Property1 {get; set;} }
I have the following classes: public class Person { public String FirstName { set;
I have the following (simplified) classes: public class Person { public string Id {get;
I have the following classes public class Person { public virtual int Id {
I have the following classes public class Person { public virtual int Id {
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.