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

The Archive Base Latest Questions

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

A majority of the examples I see now are either using the Code First

  • 0

A majority of the examples I see now are either using the Code First Approach or using an older version of MVC and the Entity Framework.

Assume I have a movie to update and I get to the Edit View, in the Edit method with the Post verb, what is the proper way to update a Movie? The first Edit Method below gets me to the Edit View with the populated Movie values and the second one is the one I want to use to update, I have tried some things, but nothing updates the data.

 public ActionResult Edit(int id)
        {
            var movie = (from m in _db.Movies1
                         where m.Id == id
                         select m).First();

            return View(movie);
        }

    [HttpPost]
    public ActionResult Edit(Movie movie)
    {
        try
        {
            // TODO: Add update logic here

           //What do I need to call to update the entity?

            _db.SaveChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }
  • 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-28T01:14:08+00:00Added an answer on May 28, 2026 at 1:14 am

    Assuming that _db is derived from ObjectContext you have two options:

    • Change the state of the entity to Modified:

      _db.Movies1.Attach(movie);
      _db.ObjectStateManager.ChangeObjectState(movie, EntityState.Modified);
      _db.SaveChanges();
      

      This marks all properties of movie as modified and will send an UPDATE statement to the database which includes all column values, no matter if the values really changed or not.

    • Reload the original entity from the database and apply the changes to it:

      var originalMovie = (from m in _db.Movies1
                           where m.Id == movie.Id
                           select m).First();
      // You actually don't need to assign to a variable.
      // Loading the entity into the context is sufficient.
      
      _db.Movies1.ApplyCurrentValues(movie);
      _db.SaveChanges();
      

      ApplyCurrentValues will mark only those properties as modified which really did change compared to the original and the UPDATE statement which will be sent to the database only includes the changed column values. So, the UPDATE statement is potentially smaller than in the first example but you have to pay the price to reload the original entity from the database.

    Edit

    How does the second code example work?

    • When you run a query using the context (_db) Entity Framework does not only retrieve the entity from the database and assign it to the left side of the query (originalMovie) but it actually stores a second reference internally. You can think of this internal context “cache” as a dictionary of key-value pairs – the key is the entity primary key and the value is the entity itself, the same object as originalMovie refers to.

    • ApplyCurrentValues(movie) looks up this entity in the context’s internal dictionary: It takes the key property value Id of the passed in movie, searches for an entity with that key in the internal dictionary and then copies property by property from the passed in (“detached”) movie to the internal (“attached”) entity with the same key. EF’s change tracking mechanism marks the properties as Modified which were actually different to create later the appropriate UPDATE statement.

    Because of this internal reference to the original entity you do not need to hold your own reference: That’s the reason why originalEntity is not used in the code. You can in fact remove the assignment to the local variable altogether.

    The example would not work if you disable change tracking when you load the original entity – for example by setting _db.Movies1.MergeOption = MergeOption.NoTracking;. The example relies on enabled change tracking (which is the default setting when entities are loaded from the database).

    I cannot say which of the two examples has better performance. That might depend on details like size of the entities, number of properties which have been changed, etc.

    It’s worth to note though that both approaches do not work if related entities are involved (for example movie refers to a category entity) and if the relationship or the related entity itself could have been changed. Setting the state to Modified and using ApplyCurrentValues both affect only scalar and complex properties of movie but not navigation properties.

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

Sidebar

Related Questions

I see the majority of WPF Ribbon examples out there use some code like
The pattern I see in the majority of .NET MVC examples is to have
I have done some research, and majority of the examples I have found use
In the majority of cases I see Log instances declared as follows: public static
What is the difference between using AuthComponent::allowedActions and AuthComponent::allow? When I Google, I see
The majority of pyparsing examples that I have seen have dealt with linear expressions.
I'm a newbie to Entity Framework, so I'd like to try something simple to
I'm using PHP Markdown (version 1.0.1n, updated October 2009) to display text saved to
Using exceptions for normal flow of code is bad - it's slow, it's bad
I have a Html Helper file for my ASP.NET MVC application. The majority of

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.