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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:32:42+00:00 2026-05-17T20:32:42+00:00

I have a entity that I retrieve as follows and is detached from the

  • 0

I have a entity that I retrieve as follows and is detached from the context:

ctx.Reviews.MergeOption = MergeOption.NoTracking;

Review review = (from r in ctx.Reviews.Include("ReviewNotes")
                 where r.ReviewID == reviewID
                 select r).First();

I then make changes to an object in the relationship:

if (review.ReviewNotes.Count > 0)
{
  ReviewNote r = review.ReviewNotes.ElementAt(0);
  r.Note = "Ugg " + DateTimeOffset.Now.ToString();
  r.CreatedDate = DateTimeOffset.Now;
}

I then attach the Object and loop the children and change it’s entity state if needed. When save changes is done nothing is updated.:

ctx.Reviews.Attach(review);
foreach (ReviewNote item in review.ReviewNotes)
{
   if (item.ReviewNoteID == 0)
   {
       ctx.ObjectStateManager.ChangeObjectState(item, EntityState.Added);
   }
   else
   {
       key = ctx.CreateEntityKey("ReviewNotes", item);
       if (ctx.TryGetObjectByKey(key, out original))
       {
           ctx.ApplyCurrentValues<ReviewNote>(key.EntitySetName, item);
       }

   }
 }

ctx.ObjectStateManager.ChangeObjectState(review, EntityState.Modified);
ctx.SaveChanges();
  • 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-17T20:32:43+00:00Added an answer on May 17, 2026 at 8:32 pm

    Because you start with MergeOption.NoTracking your entities are Detached. Then you modify and attached them. You should be aware that Attach results in an Unchanged EntityState — that is, it has not changed since it was attached to the context. So that both the original and current values have the same set of values: the modified ones.
    That’s why it does not get updated once you call SaveChanges method.

    I think you also misunderstood the purpose of ApplyCurrentValues method:


    It will take the values of the provided detached entity and use its EntityKey to locate the same entity in the context. Then it will replace the attached entity’s current scalar values with the property values from the detached entity.

    In your case where you already attached the detached entity, you don’t really need to call it, instead you need to Change the state of your ReviewNote entity to Modified so that EF will execute appropiate update methods against your data store:

    ctx.Reviews.Attach(review);
    foreach (ReviewNote item in review.ReviewNotes) {
       if (item.ReviewNoteID == 0) {
           ctx.ObjectStateManager.ChangeObjectState(item, EntityState.Added);
       }
       else {
           key = ctx.CreateEntityKey("ReviewNotes", item);
           if (ctx.TryGetObjectByKey(key, out original)) {
               // ctx.ApplyCurrentValues(key.EntitySetName, item);
               ctx.ObjectStateManager.ChangeObjectState(item, EntityState.Modified);
           }
       }
     }
    ctx.ObjectStateManager.ChangeObjectState(review, EntityState.Modified);
    ctx.SaveChanges();
    

    EDIT:
    Another approach would be to run the same query and get the ReviewNote objects into the memory and then call ApplyCurrentValues on each of them so that only the ones that has a changed property would go into Modified state:

    // This time you don't need to attach:
    //ctx.Reviews.Attach(review);
    // Make sure you have them in the memory:
    Review review2 = (from r in ctx.Reviews.Include("ReviewNotes")
                     where r.ReviewID == reviewID
                     select r).First();
    foreach (ReviewNote item in review.ReviewNotes) {
       if (item.ReviewNoteID == 0) {
           ctx.ReviewNotes.AddObject(item);
       }
       else {
           key = ctx.CreateEntityKey("ReviewNotes", item);
           if (ctx.TryGetObjectByKey(key, out original)) {
               // Note that the item is a detached object now: 
               ctx.ApplyCurrentValues(key.EntitySetName, item);
           }
       }
     }
    ctx.ObjectStateManager.ChangeObjectState(review, EntityState.Modified);
    ctx.SaveChanges();
    

    Also note that ApplyCurrentValues does only work with the scalar properties on a single entity and would not take navigation properties into account, otherwise we would just call it one time on Review object without having to go into a loop to apply it on each and every ReviewNote.

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

Sidebar

Related Questions

Say I have an entity that looks as follows public Order OrderEntity { EntityRef<Customer>
Can I create an entity that will retrieve data from 2 (or more) tables?
I have an Entity that holds the last instance of a Component: @Entity public
I have an entity that has a few relations. I need to clone this
I have an entity that has a collection in it. The collection is a
I have an entity that contains a list of other entities. The list of
Imagine you have an entity that has some relations with other entities and you
Let's say we have an entity that has attributes att1 and att2, where att1
in an app i have an entity that contains a list of other entities
i have an entity Entity1 that have one to many relation with Entity2 as

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.