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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:40:39+00:00 2026-06-07T03:40:39+00:00

Problem I want to save the attributes of a model that have changed when

  • 0

Problem

I want to save the attributes of a model that have changed when a user edits them. Here’s what I want to do …

  1. Retrieve edited view model
  2. Get domain model and map back updated value
  3. Call the update method on repository
  4. Get the “old” domain model and compare values of the fields
  5. Store the changed values (in JSON) into a table

However I am having trouble with step number 4. It seems that the Entity Framework doesn’t want to hit the database again to get the model with the old values. It just returns the same entity I have.

Attempted Solutions

I have tried using the Find() and the SingleOrDefault() methods, but they just return the model I currently have.

Example Code

private string ArchiveChanges(T updatedEntity)
{
    //Here is the problem!
    //oldEntity is the same as updatedEntity
    T oldEntity = DbSet.SingleOrDefault(x => x.ID == updatedEntity.ID);

    Dictionary<string, object> changed = new Dictionary<string, object>();

    foreach (var propertyInfo in typeof(T).GetProperties())
    {
        var property = typeof(T).GetProperty(propertyInfo.Name);

        //Get the old value and the new value from the models
        var newValue = property.GetValue(updatedEntity, null);
        var oldValue = property.GetValue(oldEntity, null);

        //Check to see if the values are equal
        if (!object.Equals(newValue, oldValue))
        {
            //Values have changed ... log it
            changed.Add(propertyInfo.Name, newValue);
        }
    }

    var ser = new System.Web.Script.Serialization.JavaScriptSerializer();
    return ser.Serialize(changed);
}

public override void Update(T entityToUpdate)
{
    //Do something with this
    string json = ArchiveChanges(entityToUpdate);

    entityToUpdate.AuditInfo.Updated = DateTime.Now;
    entityToUpdate.AuditInfo.UpdatedBy = Thread.CurrentPrincipal.Identity.Name;

    base.Update(entityToUpdate);
}
  • 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-06-07T03:40:42+00:00Added an answer on June 7, 2026 at 3:40 am

    The issue is that Entity Framework cache’s the objects it reads in the DbSet. So when you request the object the second time, it isn’t going to the database because it already has loaded it.

    However, the good news is that Entity automatically tracks the original values. See this question for information on how to get them: How to get original values of an entity in Entity Framework?

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

Sidebar

Related Questions

I have an NSManagedObject with attributes that a user can edit with a view.
I have a User model that has a :credits attribute. I want a simple
Let's say I want to prompt the user before allowing them to save a
I have an article model and a user model. The user can follow that
In a java serialization problem, I want to save some classes name and I
Problem I want to create an application that can be extended somehow by programmers.
The Problem I want to press a key when I have a line highlighted
I have 3 model the first user.rb: class User has_many :boards, dependent: :destroy has_many
I have a Rails model with various attributes and has_many relationships to other models.
Im new in this JavaScript world, but i have this problem i want to

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.