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

  • Home
  • SEARCH
  • 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 6164809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:03:27+00:00 2026-05-23T22:03:27+00:00

After researching all day and night, I have something that is currently working. However,

  • 0

After researching all day and night, I have something that is currently working. However, I am not sure I really understand what’s going on with navigation properties and entity relationships, so I’m concerned that my code might cause problems in the future. I had to manually set the navigation properties to “EntityState.Modified”. My model may eventually have many levels of navigation objects and collections. Is there an easier way to update the related entities? If there is no easier way, is this approach okay?

Here is the view model

public class ViewModel {
public ViewModel() { }
public ViewModel(Context context) {
this.Options = new SelectList(context.Options, "Id", "Name");
}
public Parent Parent { get; set; }
public SelectList Options { get; set; }
}

entity classes

public class Parent {
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual ChildOne ChildOne { get; set; }
    public virtual ChildTwo ChildTwo { get; set; }
}
public class ChildOne {
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Parent Parent { get; set; }
    public virtual int OptionId { get; set; }
    public virtual Option Option { get; set; }
}
public class ChildTwo {
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual Parent Parent { get; set; }
    public virtual int OptionId { get; set; }
    public virtual Option Option { get; set; }
}
public class Option {
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual ICollection<ChildOne> ChildrenOnes { get; set; }
    public virtual ICollection<ChildTwo> ChildrenTwos { get; set; }
}

context

public class Context : DbContext {
    public DbSet<Parent> Parents { get; set; }
    public DbSet<ChildOne> ChildrenOnes { get; set; }
    public DbSet<ChildTwo> ChildrenTwos { get; set; }
    public DbSet<Option> Options { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder) {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<Parent>()
            .HasOptional(x => x.ChildOne)
            .WithOptionalPrincipal(x => x.Parent);
        modelBuilder.Entity<Parent>()
            .HasOptional(x => x.ChildTwo)
            .WithOptionalPrincipal(x => x.Parent);
    }
}

controller

private Context db = new Context();

public ActionResult Edit() {
    ViewModel viewmodel = new ViewModel(db);
    viewmodel.Parent = db.Parents.Find(1);
    return View(viewmodel);
}

public void Save(Parent parent) {
    if (ModelState.IsValid) {
        db.Entry(parent).State = EntityState.Modified;
        db.Entry(parent.ChildOne).State = EntityState.Modified;
        db.Entry(parent.ChildTwo).State = EntityState.Modified;
        db.SaveChanges();
    }
}

and view

@model MvcApp7.Models.ViewModel

<div id="Parent">
    @Html.HiddenFor(model => model.Parent.Id)
    @Html.TextBoxFor(model => model.Parent.Name)
    <div id="ChildOne">
        @Html.HiddenFor(model => model.Parent.ChildOne.Id)
        @Html.TextBoxFor(model => model.Parent.ChildOne.Name)
        @Html.DropDownListFor(model => model.Parent.ChildOne.OptionId, Model.Options)
    </div>
    <div id="ChildTwo">
        @Html.HiddenFor(model => model.Parent.ChildTwo.Id)
        @Html.TextBoxFor(model => model.Parent.ChildTwo.Name)
        @Html.DropDownListFor(model => model.Parent.ChildTwo.OptionId, Model.Options)
    </div>
</div>

<input id="SaveButton" type="button" value="save" />
<script type="text/javascript">
    $('#SaveButton').click(function () {
        var data = $('input, select, textarea').serialize();
        $.post('@Url.Action("Save")', data, function () { });
    });
</script>
  • 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-23T22:03:28+00:00Added an answer on May 23, 2026 at 10:03 pm

    Yes you are doing it right. When working with detached entities like in web application you must tell EF exactly what state each entity has. In your scenario you will first call:

    db.Entry(parent).State = EntityState.Modified;
    

    In EFv4.1 this operation causes that parent is attached to context. Only attached entities can be persisted to database when SaveChanges is called. The state of the entity is set to modified so context will try to update existing record in the database when persisting the entity. There is one more important thing which happened when you called that statement: All related entities are attached as well but their state is set to unchanged. You must manually set correct state of all related entities because EF doesn’t know which one is new, modified or deleted. That is why your following calls are correct as well.

    Edit:

    Be aware that this updates values in parent and child but it doesn’t update relation itself. If you swap one child with another the process is much more complicated when using independent associations.

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

Sidebar

Related Questions

I've been researching all over for this, basically I have a table that displays
I've been researching this all morning and have decided that as a last-ditch effort,
so I have been researching this all morning- and I'm pretty sure the code
I have a query that is working just fine however it is a bit
After researching a bit how the different way people slugify titles, I've noticed that
After researching various hosts, I still get the feeling that it is somewhat impossible
I have a currently running PHP application that I want to add real-time feed
... ok ... so ... i just needed to clear cache after all that.
I've been researching this all day and can't seem to find an answer so
I currently have an asp.net website hosted on two web servers that sit behind

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.