What is the equivalent of ObjectContext.ApplyCurrentValues for DbContext?
Definition. DBContext is a wrapper of ObjectContext that exposes the most commonly used features of ObjectContext. In contrast, Object Context is a class of the core Entity framework API that allows performing queries and tracking the updates made to a database using strongly typed entity classes.
If you need to get ObjectContext you can cast your DbContext instance to IObjectContextAdapter interface (it is implemented explicitly) and wrapped ObjectContext instance will be available: ObjectContext context = ((IObjectContextAdapter)db).
There is no equivalent. You can either get the ObjectContext with...
((IObjectContextAdapter)myDbContext).ObjectContext.ApplyCurrentValues(...)
...or use a similar method of DbEntityEntry
:
myDbContext.Entry(originalEntity).CurrentValues.SetValues(changedEntity);
originalEntity
represents the object before the change (usually fetched from database before you update). It must be attached to the context. changedEntity
represents the entity with the same key which has been changed.
This second approach is probably closely related to the ObjectStateEntry.ApplyCurrentValues
method of EF 4.0 .
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