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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:24:05+00:00 2026-05-31T05:24:05+00:00

I am currently using EF4.3 and Code First. Creation of my objects works (via

  • 0

I am currently using EF4.3 and Code First. Creation of my objects works (via my views – just using the auto-generated Create), but when I attempt to edit an object, it does not save any changes that, utlimately, tie back to my navigation properties. I have been reading on relationships, but I don’t understand how to tell my context that the relationship has changed.

Here is some example code of my implementation.

@* Snippet from my view where I link into my ViewModel. *@
<div class="row">
    <div class="editor-label">
        @Html.LabelFor(model => model.ManagerID)
    </div>
    <div class="editor-field">
        @Html.DropDownListFor(model => model.ManagerID, ViewBag.Manager as SelectList, String.Empty)
        @Html.ValidationMessageFor(model => model.ManagerID)
    </div>
</div>

Here is my Controller implementation (POST of my Edit):

    [HttpPost]
    public ActionResult Edit(ProjectViewModel projectViewModel)
    {
        if (ModelState.IsValid)
        {
            Project project = new Project();
            project.ProjectID = projectViewModel.ProjectID;
            project.Name = projectViewModel.Name;
            project.ProjectManager = repository.GetUser(projectViewModel.ManagerID);
            repository.InsertOrUpdateProject(project);
            repository.Save();
            return RedirectToAction("Index");
        }
        ViewBag.Manager = new SelectList(repository.GetUsers(), "UserID", "FullName", projectViewModel.ManagerID);
        return View(projectViewModel);
    }

Within my Project object:

public class Project
{
    public int ProjectID { get; set; }
    [Required]
    public string Name { get; set; }

    // Navigation Properties
    public virtual User Manager { get; set; }
}

Here is the corresponding method from the repository (where my context resides):

public void InsertOrUpdateProject(Project project)
    {
        if (program.ProjectID == default(int))
        {
            context.Projects.Add(project);
        }
        else
        {
            context.Entry(project).State = EntityState.Modified;
        }
    }

Just to be clear, this does work to update my properties, but it does not update my navigation properties (in this case, Manager). Appreciate any help.

  • 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-31T05:24:06+00:00Added an answer on May 31, 2026 at 5:24 am

    Setting the state to Modified only marks scalar properties as modified, not navigation properties. You have several options:

    • A hack (you won’t like it)

      //...
      else
      {
          var manager = project.Manager;
          project.Manager = null;
          context.Entry(project).State = EntityState.Modified;
          // the line before did attach the object to the context
          // with project.Manager == null
          project.Manager = manager;
          // this "fakes" a change of the relationship, EF will detect this
          // and update the relatonship
      }
      
    • Reload the project from the database including (eager loading) the current manager. Then set the properties. Change tracking will detect a change of the manager again and write an UPDATE.

    • Expose a foreign key property for the Manager navigation property in your model:

      public class Project
      {
          public int ProjectID { get; set; }
          [Required]
          public string Name { get; set; }
      
          public int ManagerID { get; set; }
          public virtual User Manager { get; set; }
      }
      

      Now ManagerID is a scalar property and setting the state to Modified will include this property. Moreover you don’t need to load the Manager user from the database, you can just assign the ID you get from your view:

      Project project = new Project();
      project.ProjectID = projectViewModel.ProjectID;
      project.Name = projectViewModel.Name;
      project.ManagerID = projectViewModel.ManagerID;
      repository.InsertOrUpdateProject(project);
      repository.Save();
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using EF4 Code First (CTP5) to generate my database and I am
I'm using Ninject with an MVC app, also using EF4.1 Code First. I'm getting
I'm currently working on an app using EF 4.1 Code First and have a
Currently using Google Analytics as a supplement to our paid tracking software, but neither
Currently using MySQL version 5.1.6 This is my first real world build and so
I'm currently using the awesome attachment-fu plugin for a Rails app, but as a
I am using EF4. Just a quick question, is this a really good framework?
I'm using EF 4.1 Code First, and I'm making a configurable utility for parsing/importing
we are currently building our first large application with Silverlight 4 (using PRISM) and
I have just transferred my EF 4.0 Solution to use EF 4.1 Code-First. Everything

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.