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 6720699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:14:35+00:00 2026-05-26T09:14:35+00:00

I’m trying to update an object that I have previously saved with EntityFramework 4.1

  • 0

I’m trying to update an object that I have previously saved with EntityFramework 4.1 (CodeFirst)

The class Job has the following properties …

public class Job
{
    [key]
    public int Id { get; set; }
    public string Title { get; set; }
    public Project Project { get; set; }
    public JobType JobType { get; set; }
    public string Description { get; set; }
}

The initial create works fine, but the update only commits changes to the strings..

If I change the child objects eg the JobType Property from JobTypeA to JobTypeB – the change is not committed …

I’m not looking to commit a change to JobType – only to Job.

using (var context = new JobContext())
{
    context.Jobs.Attach(job);
    context.Entry(job).State = EntityState.Modified;
    context.SaveChanges();
}

Having a look at SQL Profiler – the Ids are not even being sent for the Update – however they are for the initial insert!

  • 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-05-26T09:14:35+00:00Added an answer on May 26, 2026 at 9:14 am

    Setting the state to Modified only updates scalar and complex properties, not your navigation properties. This only goes through Entity Framework’s change detection. It means that you need to load the original from the database:

    using (var context = new JobContext())
    {
        var originalJob = context.Jobs.Include(j => j.JobType)
            .Single(j => j.Id == job.Id);
    
        // Update scalar/complex properties
        context.Entry(originalJob).CurrentValues.SetValues(job);
    
        // Update reference
        originalJob.JobType = job.JobType;
    
        context.SaveChanges();
    }
    

    You could probably also leverage some “tricks” in your case:

    using (var context = new JobContext())
    {
        var jobType = job.JobType;
        job.JobType = null;
    
        context.JobTypes.Attach(jobType);
        context.Jobs.Attach(job);
        // change detection starts from here,
        // EF "thinks" now, original is JobType==null
    
        job.JobType = jobType;
        // change detection will recognize this as a change
        // and send an UPDATE to the DB
    
        context.Entry(job).State = EntityState.Modified; // for scalar/complex props
    
        context.SaveChanges();
    }
    

    It wouldn’t work though if you want to set JobType to null.

    This is a typical situation which is getting much simpler if you expose foreign keys as properties in your model: With a JobTypeId in your Job entity your code would work because the FK property is scalar and setting the state to Modified will also mark this property as modified.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to create an if statement in PHP that prevents a single post
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I am doing a simple coin flipping experiment for class that involves flipping a

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.