Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of ObjectContext.ApplyCurrentValues for DbContext

What is the equivalent of ObjectContext.ApplyCurrentValues for DbContext?

like image 650
Josh Avatar asked Apr 27 '11 16:04

Josh


People also ask

What is DBContext and ObjectContext?

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.

Which interface you have to implement to get the reference of ObjectContext from DBContext?

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).


1 Answers

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 .

like image 50
Slauma Avatar answered Oct 13 '22 11:10

Slauma