Which code segment should you insert at line 05?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
You use the ADO.NET Entity Framework to model entities. You write the following code segment.
(Line numbers are included for reference only.)

01 public partial class SalesOrderDetail : EntityObject
02 {
03 partial void OnOrderQtyChanging(short value)
04 {
05 …
06 {
07 …
08 }
09 }
10 }

You need to find out whether the object has a valid ObjectStateEntry instance. Which code segment should you insert at line 05?

You use Microsoft Visual Studio 2010 and Microsoft .NET Framework 4.0 to create an application.
You use the ADO.NET Entity Framework to model entities. You write the following code segment.
(Line numbers are included for reference only.)

01 public partial class SalesOrderDetail : EntityObject
02 {
03 partial void OnOrderQtyChanging(short value)
04 {
05 …
06 {
07 …
08 }
09 }
10 }

You need to find out whether the object has a valid ObjectStateEntry instance. Which code segment should you insert at line 05?

A.
if (this.EntityState != EntityState.Detached)

B.
if (this.EntityState != EntityState.Unchanged)

C.
if (this.EntityState != EntityState.Modified)

D.
if (this.EntityState != EntityState.Added)

Explanation:
Detached The object exists but is not being tracked. An entity is in this state immediately after it has been created and before it is added to the
object context. An entity is also in this state after it has been removed from the context by calling the Detach method or if it is loaded by
using a NoTracking MergeOption. There is no ObjectStateEntry instance associated with objects in the Detached state.
Unchanged The object has not been modified since it was attached to the context or since the last time that the SaveChanges method was called.
Added The object is new, has been added to the object context, and the SaveChanges method has not been called.
After the changes are saved, the object state changes to Unchanged. Objects in the Added state do not have original values in the
ObjectStateEntry.
Deleted The object has been deleted from the object context. After the changes are saved, the object state changes to Detached.
Modified One of the scalar properties on the object was modified and the SaveChanges method has not been called.
In POCO entities without change-tracking proxies, the state of the modified properties changes to Modified when the DetectChanges
method is called. After the changes are saved, the object state changes to Unchanged.

EntityState Enumeration
(http://msdn.microsoft.com/en-us/library/system.data.entitystate.aspx)

CHAPTER 6 ADO.NET Entity Framework
Lesson 1: What Is the ADO.NET Entity Framework?
Storing Information about Objects and Their State (page 381)



Leave a Reply 4

Your email address will not be published. Required fields are marked *


Mike

Mike

“There is no ObjectStateEntry instance associated with objects in the Detached state.” So the answer is A surely? Added objects have CurrentValues even if they don’t have OriginalValues

Andi

Andi

I agree with you. I can’t understand this question.

Pavel

Pavel

For me the answer is also A, though in demo exams the answer is D.

John Galt

John Galt

Who’s validating these questions? Clearly the answer is A here, as another poster already pointed out you even have the reason why specified in the answer:

Detached:
“There is no ObjectStateEntry instance associated with objects in the Detached state.”

And the question asks:
“find out whether the object has a valid ObjectStateEntry instance”

This COULDN’T be any clearer. The answer is A, because Detached doesn’t have a valid ObjectStateEntry instance.