Is the entity structure mapping assigned values ​​to the original to determine the IsModified flag?

If I load an entity object and then set one of the properties to the same value as before, does it detect changes in the framework or sets the IsModified flag to true?

Here's how the generated code for the Name field looks like this:

OnNameChanging(value);
ReportPropertyChanging("Name");
_Name = StructuralObject.SetValidValue(value);
ReportPropertyChanged("Name");
OnNameChanged();

I do not know which of these events set the IsModified flag for this field and for the entire object.

+5
source share
2 answers

Your context only tracks if your data has been changed, not if it is different.

You can do the check as follows:

  private void CheckIfDifferent(DbEntityEntry entry)
    {
        if (entry.State != EntityState.Modified) 
            return;

        if (entry.OriginalValues.PropertyNames.Any(propertyName => !entry.OriginalValues[propertyName].Equals(entry.CurrentValues[propertyName])))
            return;

       (this.dbContext as IObjectContextAdapter).ObjectContext.ObjectStateManager.GetObjectStateEntry(entry.Entity).ChangeState(EntityState.Unchanged);
    }

source: fooobar.com/questions/598081 / ...

+3
source

, - (EF6). , , if , , " ". : :

var things = dbContext.Things.AsQueryable();
var thing = things.First();
string name = thing.Name;
thing.Name = name;
var entry = dbContext.Entry(thing);
var state = entry.State;
int count = dbContext.ChangeTracker.Entries().Count(e => e.State == EntityState.Modified);
var modified = entry.Property(x => x.Name).IsModified;
+2

All Articles