ObjectStateManager vs Entry().State

entity-framework entity-framework-4 entity-framework-6

Question

What distinguishes these two, and which is preferred? using modify object stateEntry().state

db.ObjectStateManager.ChangeObjectState(employeeFromDB, EntityState.Modified)

vs

context.Entry(employeeFromDB).State = EnitityState.Modified;
1
2
10/7/2014 4:30:06 AM

Accepted Answer

You are essentially calling a function on the underlyingSystem.Data.Objects.ObjectStateManager Rather than getting and altering a Property directly through the DbContext, you should use your DbContext class.

The first illustrationChangeObjectState() can only be employed to change anObjectStateEntry of an entity that is already present in the situation. An exception is thrown if you attempt to edit something that doesn't exist.

Verify the Exceptions in ObjectStateManager.ChangeObjectState.

In the second case, if the object is introduced but doesn't already exist in the context, you will still receive an exception.db.saveChanges() however, by simply making a change, the same strategy may be used to add a new record.EntityState.Modified to EntityState.Added

You must also use your DbContext to retrieve the underlying ObjectContext((IObjectContextAdapter)context).ObjectContext

3
1/11/2017 9:01:35 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow