Entity Framework 5 - Why is Entity State “Modified” after PropertyValue is set back to Original

后端 未结 3 1913
长发绾君心
长发绾君心 2021-01-11 23:59

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.

         


        
3条回答
  •  孤独总比滥情好
    2021-01-12 00:38

    Thx for the hint :)

    here is my EF5 (DbContext) Solution. I call this method for every DbEnityEntry that i get from ChangeTracker.Entries()

        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);
        }
    

提交回复
热议问题