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 ?
Is there a way to get the original Entity itself from the ChangeTracker (rather than just the original values)? If the State is Modified , then I suppose I could do this: // Get the DbEntityEntry from the DbContext. ChangeTracker... // Store the current values var currentValues = entry.
The DbEntityEntry enables you to access the entity state, and current and original values of all properties of a given entity. The following example code shows how to retrieve important information of a particular entity.
The Find method on DbSet uses the primary key value to attempt to find an entity tracked by the context. If the entity is not found in the context then a query will be sent to the database to find the entity there. Null is returned if the entity is not found in the context or in the database.
@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);
You can access them through ObjectStateEntry
var originalValues = context
.ObjectStateManager.GetObjectStateEntry(myEntity).OriginalValues;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With