GetModifiedProperties not working for EF4.1 DbContext

With ObjectContext:

var objContext = new ObjContextEntities(); var accountType = objContext.AccountTypes.FirstOrDefault(x => x.Id == 0); accountType.Name = "ABC"; var stateEntry = objContext.ObjectStateManager.GetObjectStateEntry(accountType); Console.WriteLine(stateEntry.GetModifiedProperties().Count()); //--------> Outputs 1 as expected 

With DbContext:

 var dbContext = new DbContextEntities(); var accountType = dbContext.DBAccountTypes.FirstOrDefault(x => x.Id == 0); accountType.Name = "XYZ"; var dbObjContext = ((IObjectContextAdapter)dbContext).ObjectContext; var stateEntry = dbObjContext.ObjectStateManager.GetObjectStateEntry(accountType); Console.WriteLine(stateEntry.GetModifiedProperties().Count()); //--------> Outputs 0 

I would like to switch to using DbContext, but I have code that depends on this function. Is this a known bug? Can anyone suggest an alternative approach? Thanks.

+4
source share
1 answer

Ok, this seems to be a trick:

 var dbContext = new DbContextEntities(); var accountType = dbContext.DBAccountTypes.FirstOrDefault(x => x.Id == 0); accountType.Name = "XYZ"; var entry = dbContext.Entry(accountType); var modifiedProperties = entry.CurrentValues.PropertyNames.Where(propertyName => entry.Property(propertyName).IsModified).ToList(); Console.WriteLine(modifiedProperties.Count()); //--------> Outputs 1 

More useful information here: http://msdn.microsoft.com/en-US/data/jj592677

+8
source

All Articles