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 ?
相关问题
- How to Specify a Compound key in Entity Framework
- ToList method not available for TrackableCollectio
- Getting “Cannot insert the value NULL into column”
- Doctrine return null in place of EntityNotFoundExc
- How can I implement Query Interception in a LINQ t
相关文章
- How do we alias a Sql Server instance name used in
- entity type has no key defined - Code first
- POCO Vs Entity Framework Generated Classes? [close
- The 'DbProviderFactories' section can only
- Is SaveChanges() Necessary with Function Imports (
- Entity Framework - An item with the same key has a
- Is there a way to modify the entity mapping config
- Setup EF4 data source for SQL Compact 4
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.
This could be refined further to the following:
The
Where
in the above, good, response is not needed.There are a few versions of Entity Framework in use.
I myself prefer Code First and with that API it's easy as
Docs https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbentityentry.reload(v=vs.103).aspx
The older API's have a Refresh method on the ObjectContext which can help in certain use cases
Docs https://msdn.microsoft.com/en-us/library/bb896255(v=vs.110).aspx
@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:
My working solution uses
AsNoTracking()
method fromDbSet
, like the example below:You can access them through ObjectStateEntry
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.
and then compare the clone to the changed.