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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:01:00+00:00 2026-05-22T15:01:00+00:00

I have a ASP.NET MVC 3 application, using Entity Framework 4 to handle Data

  • 0

I have a ASP.NET MVC 3 application, using Entity Framework 4 to handle Data Access.

My View Model object structure looks like this:

public class OrderViewModel 
{
  public int Id { get; set; }
/* ... */
  public List<OrderLineItemViewModel> LineItems { get; set; }
}

public class OrderLineItemViewModel 
{
  public int Id { get; set; }
  public string Name { get; set; }
}

And, my Entity Framework Model looks like this:

public class Order 
{
  public int Id { get; set; }
/* ... */
  public List<OrderLineItem> LineItems { get; set; }
}

public class OrderLineItem
{
  public int Id { get; set; }
  public string Name { get; set; }
}

The thing that I am struggling with is how to handle editing an existing Order. My controller function will convert the Parent View Model into an Order object, and also convert the children (OrderLineItemViewModel) into the Entity Model (OrderLineItem). However, upon attaching the entity to the ObjectContext, it loses state of which Line Items have been changed, added, or deleted.

All of the examples that I have found that address parent/child relationships seem to advocate not doing cascading updates to children, rather, preferring to have a seperate Controller Action and View for updating the child collection. However, I am trying to have the edit happen as one atomic operation… When the user hits “Edit Order” to submit the changes, I want the parent’s fields to update, as well as handle any new/deleted/modified children…

What’s the best approach to handling change tracking on child objects in Entity Framework, when converting from a View Model?

I have tried adding a IsNew, IsDirty, and IsDeleted to the ViewModel and the Model, then calling the appropriate function for each child (Attach, Delete or Add), but I get errors when trying to add saying that the child alreadys exists in the ObjectStateManager in an UnChanged state…

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-05-22T15:01:00+00:00Added an answer on May 22, 2026 at 3:01 pm

    You must indeed somehow tell EF what has changed, what is added and what is deleted. There are two approaches:

    • Create your Order and OrderLines from your view models and Attach the Order instance to the context. After that you must change state of each entity (context.ObjectStateManager.ChangeObjectState).
    • Load the whole Order with OrderLines from the database first and then merge changes from view models to attached entity graph – that is what I use.

    More complex description is in another answer. This whole become much worse when you work with many-to-many or one-to-many relations where related entities can change parent.

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

Sidebar

Related Questions

I have an ASP.NET MVC 3 Web Application using Linq-to-SQL for my data access
General Info: ASP.NET MVC app using ADO.NET Entity Framework SQL Server 2005 I have
First some brief background: I have an existing ASP.NET MVC 1 application using Entity
I am using asp.net MVC to develop an application that will have ajax interactions.
I am writing an asp.net mvc application to learn Entity Framework with and am
I am developing a medium sized ASP.NET project using ASP.NET MVC and Entity Framework.
I have developed a fairly small asp.net MVC application using the repository pattern and
I have an ASP.NET MVC-application which I want deployable on both IIS6 and IIS7
I have an asp.net mvc application with a route similar to: routes.MapRoute(Blog, {controller}/{action}/{year}/{month}/{day}/{friendlyName}, new
I have an ASP.NET MVC application, when a user clicks on the submit button

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.