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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:36:14+00:00 2026-05-28T03:36:14+00:00

I know that using ObjectStateManager just results in 1 trip to the database, but

  • 0

I know that using ObjectStateManager just results in 1 trip to the database, but a larger update statement and ApplyCurrentValues results in 2 trips to the database, one to retreive the entity and one to update it, so what are the pros and cons of each also, how does issuing the following statement know that I want to update that current record, I know it has something to do with loading it into context, but not sure how it works.

[HttpPost]
public ActionResult Edit(Movie movie)
{

    (from m in _db.Movies1
     where m.Id == movie.id
     select m).First()

     //How does calling the above query know to update the movie, 
       can't I do it with out it?
     _db.Movies1.ApplyCurrentValues(movie);
     _db.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-28T03:36:15+00:00Added an answer on May 28, 2026 at 3:36 am

    The code you posted ‘knows’ you want to update the record because it assumes if the state is modified (which applycurrentvalues will mark properties as modified) then you must want to update that field.

    So anything that is modified is set, and then sent to the database. However the code that has .First() in it does nothing helpful here.

    ApplyCurrentValues does not load anything, so your first line of code loads it into the context. Then you ApplyCurrentValues merges the values into it. Anything that is different is marked as modified and copied over then those fields only are sent to the db for update.

    You can profile the activity, check out
    http://msdn.microsoft.com/en-us/magazine/gg490349.aspx

    also there is a nice demp profiler available at http://efprof.com/

    Another option is to simply attach as modified and save changes. I think that will only yield one query (the update to the database), I’ll have to double check that.
    That code would look like:

    _db.Entry(movie).State = EntityState.Modified; //attaches is as well
    _db.SaveChanges();
    

    That will save every property, but I think it may be one less trip (update, reload, as opposed to load, update, reload)

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

Sidebar

Related Questions

I know that using SharePoint internally is free, but what if I create an
I know that using Dim currUser As String = Request.ServerVariables(LOGON_USER) returns the Domain\Username, but
I know that just using rand() is predictable, if you know what you're doing,
I know that C# has the using keyword, but using disposes of the object
I know that I can share files using Shared Folders in Virtual PC, but
I know that using .net VSTO there is no way to do this. but
I know that using VIM I can format C++ code just using gg=G Now
I know that using $(window).load() the code executes after everything's loaded. But it doesn't
I know that using here for a link's text is considered bad, but if
First of all, I know that using regex for email is not recommended but

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.