How to get original values of an entity in Entity Framework?

JatSing picture JatSing · Nov 15, 2011 · Viewed 27k times · Source

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 ?

Answer

Leonel Sanches da Silva picture Leonel Sanches da Silva · Jul 11, 2013

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