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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:00:54+00:00 2026-06-14T17:00:54+00:00

i use the EF5 and don’t know why a entity has the state modified

  • 0

i use the EF5 and don’t know why a entity has the state “modified” after i set the only changed PropertyValue of this entity back to the original value.

using (TestDbContext context = new TestDbContext())
        {
            string name = context.Person.First().Name;

            // count is 0
            int count = context.ChangeTracker.Entries().Count(e => e.State == EntityState.Modified);

            // Change Value
            context.Person.First().Name = "Test";

            // count is 1 
            count = context.ChangeTracker.Entries().Count(e => e.State == EntityState.Modified);

            // Revert Value
            context.Person.First().Name = name;


            context.ChangeTracker.DetectChanges();

            // count is 1 
            count = context.ChangeTracker.Entries().Count(e => e.State == EntityState.Modified);
        }

Why? 🙁

  • 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-14T17:00:56+00:00Added an answer on June 14, 2026 at 5:00 pm

    Because Entity Framework only keeps track if the data got modified, not if it’s different from it’s original content.

    We use a nifty method to reset the state to unmodified when the entity is unchanged:

        public static void CheckIfModified(EntityObject entity, ObjectContext context)
        {
            if (entity.EntityState == EntityState.Modified)
            {
                ObjectStateEntry state = context.ObjectStateManager.GetObjectStateEntry(entity);
                DbDataRecord orig = state.OriginalValues;
                CurrentValueRecord curr = state.CurrentValues;
    
                bool changed = false;
                for (int i = 0; i < orig.FieldCount && !changed; ++i)
                {
                    object origValue = orig.GetValue(i);
                    object curValue = curr.GetValue(i);
                    if (!origValue.Equals(curValue) && (!(origValue is byte[]) || !((byte[])origValue).SequenceEqual((byte[])curValue)))
                    {
                        changed = true;
                    }
                }
    
                if (!changed)
                {
                    state.ChangeState(EntityState.Unchanged);
                }
            }
        }
    

    Please note that this method is for EF 4.0, not for the newer versions with DbContext. But it is no problem to rewrite it to use EF 4.1+, I have done this myself already but I can’t find the code right now.

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

Sidebar

Related Questions

use this code, in the Preferences activity, to know when the reset preference has
I would like to use the latest Entity Framework (EF5), but not like to
Use a Content Delivery Network (CDN) Compress components with gzip Configure entity tags (ETags)
I have an entity, let's call it CommonEntity that has a primary key used
i use Entity Framework 5.0 by Model First. In 4.x there is a bug:
I'm trying to use the CodeFirst data migrations in EF5 with a table that
I am programming with EF5-code first and want to use LocalDb. How can I
This is the same exact question as this , but instead for EF5. Is
USE master GO DECLARE @DbName nvarchar(MAX) SET @DbName = N'DataBase' ALTER DATABASE @DbName SET
I'm using the latest EntityFramework 5 from NuGet. I don't know if I'm doing

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.