In EF 4.0, if I understand it right, there are two type of values in Entity : current values and original values.
We can set original values by calling ApplyOriginalValues(TEntity) method but how to get original values ?

- 4,857
- 16
- 55
- 65
7 Answers
@Eranga answer is outdated for EF 5. For some reason, EF 5 doesn't work fine when getting original values using an statement like this:
var originalValues = context.Entry(myEntity).OriginalValues;
My working solution uses AsNoTracking()
method from DbSet
, like the example below:
var originalEntity = context.MyEntities.AsNoTracking().FirstOrDefault(me => me.MyEntityID == myEntity.MyEntityID);

- 6,972
- 9
- 46
- 66
-
How to get the modified values only?? – Awais Mahmood Sep 23 '15 at 13:13
-
@AwaisMahmood I think this deserve a separate question. – Leonel Sanches da Silva Sep 23 '15 at 13:50
-
@CiganoMorrisonMendez.. kindly visit this.. I have asked this but didn't get my answer.. http://stackoverflow.com/questions/32737975/how-to-check-that-which-properties-entries-are-modified-and-which-are-not-on-edi?noredirect=1#comment53316651_32737975 – Awais Mahmood Sep 23 '15 at 13:53
-
2Please be aware that AsNoTracking will read entity from DB. It might have been changed by concurrent task in which scenario you might proceed with incorrect data... – 0xDEAD BEEF Feb 16 '16 at 14:09
-
@0xDEADBEEF Exactly. The context is not thread-safe, and for this cases is important to use Transactions, but this is another subject. – Leonel Sanches da Silva Feb 16 '16 at 14:13
-
Given that **MyEntityID** is a property on the object that can be changed like any other property this answer is totally unvalid. How can you guarantee that the user hasn't change the ID to something else making you read original values from nothing or another entity? – Håkan Edling Feb 05 '17 at 21:08
-
@HåkanEdling Disagree. Keys cannot be changed. EF raises an error when you attempt to change the field value in the database. – Leonel Sanches da Silva Feb 06 '17 at 01:16
-
Ok then @CiganoMorrisonMendez :) In that case I'm totally fine with it apart from the fact that it's hitting the database getting values already present in the change tracker. On the other hand you can have tracking disabled. – Håkan Edling Feb 06 '17 at 06:20
You can access them through ObjectStateEntry
var originalValues = context
.ObjectStateManager.GetObjectStateEntry(myEntity).OriginalValues;

- 32,181
- 5
- 97
- 96
-
originalValues is DbDataRecord type. How to convert it to entity type ? – JatSing Nov 15 '11 at 10:19
-
@Sun it does not have entity types. You need to cast the values to appropriate type. eg `var name = (string)originalValues["Name"];` – Eranga Nov 15 '11 at 10:36
This could be refined further to the following:
var originalEntity = context.MyEntities.AsNoTracking()
.FirstOrDefault(me => me.MyEntityID == myEntity.MyEntityID);
The Where
in the above, good, response is not needed.

- 61,146
- 24
- 125
- 222

- 703
- 1
- 7
- 14
var originalEntity = (EntityType)context.Entry(editEntity).OriginalValues.ToObject();
Sorry for my english. With this way you can get the original entity values in the form of the object entity with no changes on the edit values.
Example: If you like edit a Person the line in top look like this
var originalPerson = (Person)context.Entry(editPerson).OriginalValues.ToObject();

- 479
- 1
- 10
- 27

- 81
- 1
- 1
I ran into a similar problem and AsNoTracking was not an option for my situation so i came up with something that works well enough for me: first "clone" the entity then do changes.
public T Clone<T>(T entity)
where T : class, new() {
var clone = new T();
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
.Where(a => a.CanRead &&
a.CanWrite &&
a.GetMethod.IsFinal);
foreach (var property in properties) {
property.SetValue(clone, property.GetValue(entity));
}
return clone;
}
and then compare the clone to the changed.
public string GenerateChangeText<T>(T original, T current)
where T : class, new() {
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.FlattenHierarchy)
.Where(a => a.CanRead &&
a.CanWrite &&
a.GetMethod.IsFinal);
var changes = string.Empty;
foreach (var property in properties) {
var originalValue = property.GetValue(original);
var currentValue = property.GetValue(current);
if (originalValue == null && currentValue == null) continue;
if ((originalValue != null && !originalValue.Equals(currentValue)) ||
(currentValue != null && !currentValue.Equals(originalValue))) {
changes += $" changed {property} from {original ?? "NULL"} to {current ?? "NULL"}.";
}
}
return changes;
}

- 31
- 7
There are a few versions of Entity Framework in use.
I myself prefer Code First and with that API it's easy as
_context.Entry(Entity).Reload();
The older API's have a Refresh method on the ObjectContext which can help in certain use cases
ObjectContext.Refresh(RefreshMode.StoreWins, Entity);
Docs https://msdn.microsoft.com/en-us/library/bb896255(v=vs.110).aspx

- 13,690
- 3
- 53
- 61
This answer refers to Entity Framework 6. In EF 6 there is an Original value and Current value https://msdn.microsoft.com/en-us/library/gg679512(v=vs.113).aspx After looking and not finding a good answer I came up with the following test function and thought I would post it for others needing to do the same.
private void test()
{
// table has a field Description of type varchar(200)
WDMDBEntities context = new WDMDBEntities();
var query = context.Brands;
List<Brand> records = query.ToList();
if (records.Count > 0)
{
Brand currentRecord = records[0];
currentRecord.Description = "some new text";
string originalValue = null;
switch (context.Entry(currentRecord).State)
{
case System.Data.Entity.EntityState.Added:
originalValue = null;
break;
case System.Data.Entity.EntityState.Deleted:
case System.Data.Entity.EntityState.Detached:
case System.Data.Entity.EntityState.Modified:
case System.Data.Entity.EntityState.Unchanged:
originalValue = context.Entry(currentRecord).Property(u => u.Description).OriginalValue;
break;
}
}
context.Dispose();
}

- 24
- 3