Quantcast
Channel: T4
Viewing all articles
Browse latest Browse all 7

The hidden assumption of EF 4 self-tracking entity

$
0
0

The hidden assumption of EF 4 self-tracking entity

EF, Entity Framework, Self tracking

recently I was diving into the Entity Framework self-tracking entity T4 and its generated code.

doing so I was finding some interesting assumptions made about the entity behaviors.

this post will illuminate some of those assumptions.

 

Background

EF self tracking entity is one of the major enhancement of EF 4.0,

it is a disconnected model (similar to the old DataSet) which enable tracking

the entity state (Added, Modified, Deleted and Unchanged) while the entity

is detached from the EF ObjectContext.

the code snippet of this post will refer the following very simple edmx:

EF, Entity Framework, Self tracking 

Assumptions
1st assumption

by default the tracking is disable (ChangeTrackingEnabled = false).

 

2nd assumption

trying to change the entity state while the tracking is disable

Code Snippet
  1. var p = new Person();
  2. p.ChangeTracker.State = ObjectState.Unchanged;

will have no affect on the entity state (the state will remain Added which is the default

state for new entities).

the following snippet shows the code of the State property:

T4 generated code
  1. public ObjectState State
  2. {
  3.     get { return _objectState; }
  4.     set
  5.     {
  6.         if (_isDeserializing || _changeTrackingEnabled)
  7.         {
  8.             OnObjectStateChanging(value);
  9.             _objectState = value;
  10.         }
  11.     }
  12. }
 
3rd assumption

the entity enable the tracking after deserialization.

T4 generated code
  1. [OnDeserialized]
  2. public void OnDeserializedMethod(StreamingContext context)
  3. {
  4.     IsDeserializing = false;
  5.     ChangeTracker.ChangeTrackingEnabled = true;
  6. }

 

4th assumption

MarkAsAdded, MarkAsModified, MarkAsDeleted and MarkAsUnchanged all enable the trucking.

T4 generated code
  1. public static T MarkAsAdded<T>(this T trackingItem) where T : IObjectWithChangeTracker
  2. {    
  3.     trackingItem.ChangeTracker.ChangeTrackingEnabled = true;
  4.     trackingItem.ChangeTracker.State = ObjectState.Added;
  5.     return trackingItem;
  6. }

 

5th assumption

State = ObjectState.Unchanged, MarkAsUnchanged and AcceptChanges

has different behaviors:

in general State property: is only changing when tracking is enabled,

MarckAsUnchanged: enable the tracking and

AcceptChanges: clear the original values, enable the tracking and

reset the relationship tracking (added and removed related entities).

State: T4 generated code
  1. public ObjectState State
  2. {
  3.     get { return _objectState; }
  4.     set
  5.     {
  6.         if (_isDeserializing || _changeTrackingEnabled)
  7.         {
  8.             OnObjectStateChanging(value);
  9.             _objectState = value;
  10.         }
  11.     }
  12. }

 

MarkAsUnchanged: T4
  1. public static T MarkAsUnchanged<T>(this T trackingItem) where T : IObjectWithChangeTracker
  2. {    
  3.     trackingItem.ChangeTracker.ChangeTrackingEnabled = true;
  4.     trackingItem.ChangeTracker.State = ObjectState.Unchanged;
  5.     return trackingItem;
  6. }

 

AcceptChang: T4 generated code
  1. public void AcceptChanges()
  2. {
  3.     OnObjectStateChanging(ObjectState.Unchanged);
  4.     OriginalValues.Clear();
  5.     ObjectsAddedToCollectionProperties.Clear();
  6.     ObjectsRemovedFromCollectionProperties.Clear();
  7.     ChangeTrackingEnabled = true;
  8.     _objectState = ObjectState.Unchanged;
  9. }

 

Summary

the self tracking entity is a fairly young concept in the EF world

and we can expect that it will keep changing.

as I was reading in one of the Harry Potter’s book

"do not trust anything you cannot understand", so be aware of that

assumption and use it well.

 

kick it on DotNetKicks.com Shout it

Digg This

Viewing all articles
Browse latest Browse all 7

Latest Images

Trending Articles





Latest Images