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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:57:49+00:00 2026-05-26T15:57:49+00:00

It seems like supporting Entity.IsDirty should be easy with Entity Framework 4.1 with Automatic

  • 0

It seems like supporting Entity.IsDirty should be easy with Entity Framework 4.1 with Automatic Change Tracking (proxy objects) turned on. However, I have not found this to be the case so far.

public class DomainContext : DbContext, IDomainContext
{

    /// <summary>
    /// Indicates whether changes have been made to the entity with that have 
    /// not been saved. This method assumes EF Proxies are being used for change tracking.
    /// </summary>
    /// <param name="entityId">The Id of the entity.</param>
    /// <returns>True if the entity is dirty, false otherwise.</returns>
    public bool HasChanges(Guid entityId)
    {
        foreach (DbEntityEntry entry in this.ChangeTracker.Entries())
        {
            IEntity entity = entry.Entity as IEntity;
            if (entity != null)
            {
                if (entity.Id == entityId && entry.State != System.Data.EntityState.Unchanged)
                {
                    return true;
                }
            }
        }
        return false;
    }
}

The above method doesn’t seem to work when changes have been made to properties on the entity that are reference/complex properties. For instance:

public class UserPreferences : EntityBase
{

    /// <summary>
    /// Comma delimited list of AFEs for which the user is interested in viewing Events.
    /// </summary>
    public virtual string ViewableAFEs { get; set; }

    /// <summary>
    /// The primary operating area for the user. This is used for defaulting purposes.
    /// </summary>
    public virtual OperatingArea PrimaryOperatingArea
    {
        get { return _primaryOpArea; }
        set
        {
            if (_primaryOpArea != value)
            {
                _primaryOpArea = value;
                RaisePropertyChanged(() => PrimaryOperatingArea);
            }
        }
    }
}

If I new up the above class, or get an existing UserPreferences entity from the database and then change the PrimaryOperatingArea property, DomainContext.HasChanges will return false. I believe this is occuring because Entity Framework tracks complex & reference properties differently from value type properties.

Changing the ViewableAFEs property (string) does show up as a change when calling the HasChanges method above.

My question is how do I expose a generic method on my derived DomainContext that will evaluate all properties (including complex, reference, collection types) and determine if the entity is dirty?

I’m curious, have others of you using EF 4.1 leveraged EF for IsDirty functionality, or did you roll your own isDirty by using INotifyPropertyChanged, etc. ?

Thanks!

  • 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-26T15:57:50+00:00Added an answer on May 26, 2026 at 3:57 pm

    DbContext API already exposes options to check if an entity or some properties were changed.

    • to check if an entity was modified:

      var myFooHasChanges = context.Entry(myFoo).State == EntityState.Modified;

    • to check if a property was modified:

      var barWasModified = context.Entry(myFoo).Property(u => u.Bar).IsModified;

    You can read all about this in: Using DbContext in EF 4.1 Part 5: Working with Property Values

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

Sidebar

Related Questions

Seems like this should be obvious, but how do I send arrow key presses
Seems like this should be simple, but powershell is winning another battle with me.
Seems like a simple enough question but I can't seem to find the answer.
Seems like a simple problem: I have an SVN repo inside our firewall. I
Seems like the subtraction is triggering some kind of issue and the resulting value
Seems like as really simple thing to do, but I just can't track it
Seems like every C# static analyzer wants to complain when it sees a public
Seems like cuke doesn't show the full error message (at least when problem occurs
Seems like a standard approach for an ioc when given a scenario like (C#
seems like I'm stuck with jQuery tabs. I'm trying to pass selected tab name

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.