none
Self-Tracking-Entities with IEntityWithKey? RRS feed

  • Question

  • Hi @ll,

    I´am using the Entity Framework v4.0 with model first. I have read in the msdn that the IEntityWithKey will increase performance. Is it possible to implement this interface with STE by T4? I want to attach/detach entities but it is only possible when you implement this interface. Is there another way to do this? Lazy Loading is not enabled!

     

    Any help would be nice :-)

     

    Kind Regards,

    Daniel

    Tuesday, June 28, 2011 8:32 AM

Answers

  • Hi..

    Sorry.. I can't be finished with the blog post in a near future I'm missing a fundamental setting which I need to debug in an environment where I can reproduce it (which I don't have access to now).

    But, I have the modifications at hand that implements the IEntityWithKey to the template, and will describe it short here.

    All modification is done in the base .tt file (not the .context.tt file).

    First, at line 37 at position 182 the code looks like this in the original file:

    <#=entity.BaseType == null ? ": " : ", "#>
    

    In this, add IEntityWithKey, after the second : but before the " so it looks like this:

    <#=entity.BaseType == null ? ": IEntityWithKey, " : ", "#>
    

    Then you need to write the EntityKey implementation, so insert the following code after line 1098:

    	if(entity.BaseType == null)
    	{
    		region.Begin("IEntityWithKey Implementation");
    #>
    	[DataMember]
    	public EntityKey EntityKey
    	{
    		get
    		{
    			return _entityKey;
    		}
    		set
    		{
    			if (_entityKey != value)
    			{
    				_entityKey = value;
    				OnPropertyChanged("EntityKey");
    			}
    		}
    	}
    	private EntityKey _entityKey;
    <# 
    		region.End();
    	}
    

    Finally, to avoid having the state to be set to modified each time EntityKey is set (since this isn't modifying the entity) you need to modify the OnPropertyChanged for the entity class, this can be done by modifying the line at 352 from:

        if (ChangeTracker.State != ObjectState.Added && ChangeTracker.State != ObjectState.Deleted)
    

    to:

        if (propertyName.Equals("EntityKey") == false && ChangeTracker.State != ObjectState.Added && ChangeTracker.State != ObjectState.Deleted)
    

    With these changes you have IEntityWithKey implementation in your entities!



    --Rune
    Sunday, July 10, 2011 7:21 AM

All replies

  • Push again! Is it possible to implement IEntityWithKey with Self-Tracking-Entities?

     

    Kind Regards,

    Daniel

    Thursday, June 30, 2011 9:01 AM
  • Yes, it is...

    I have done it at work since I had a another problem since STE doesn't use IEntityWithKey..

    Currently writing a blog post that describes it.. So be patient :)


    --Rune
    Sunday, July 3, 2011 5:45 PM
  • Hello Daniel,

    According to your description, you used EF 4.0, I think Julie Lerman's blog here can help you somehow:

    http://thedatafarm.com/blog/data-access/knocking-off-danny-simmons-entity-framework-beta-2-list-3-4-entitykey-serialization-and-new-entity-interfaces/

    Here I quoted part of the article to make this clear:

    We are continuing down the path toward true POCO and persistence ignorance.  To that end we have split the original IPOCO interface (IEntity) into two separate, more specific interfaces (IEntityWithKey and IEntityWithChangeTracker), we made IEntityWithKey optional (not implementing it has performance implications, but the scenario is supported), and many of the ObjectServices signatures have been modified to take instances of Object rather than IEntity which sets the stage for further progress here in future releases.

    I hope this can help you.

     

    Good day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, July 4, 2011 2:10 PM
    Moderator
  • Hello Jackie,

     

    I know Julie Lerman´s blog. I only use self tracking entities (no pocos). I need an example how to implement IEntityWithKey with self tracking entities because I want to use atach/detach methods for my entities with Entity Framework v4.0.

     

    Kind Regards,

    Daniel

    Monday, July 4, 2011 2:29 PM
  • Hi..

    Sorry.. I can't be finished with the blog post in a near future I'm missing a fundamental setting which I need to debug in an environment where I can reproduce it (which I don't have access to now).

    But, I have the modifications at hand that implements the IEntityWithKey to the template, and will describe it short here.

    All modification is done in the base .tt file (not the .context.tt file).

    First, at line 37 at position 182 the code looks like this in the original file:

    <#=entity.BaseType == null ? ": " : ", "#>
    

    In this, add IEntityWithKey, after the second : but before the " so it looks like this:

    <#=entity.BaseType == null ? ": IEntityWithKey, " : ", "#>
    

    Then you need to write the EntityKey implementation, so insert the following code after line 1098:

    	if(entity.BaseType == null)
    	{
    		region.Begin("IEntityWithKey Implementation");
    #>
    	[DataMember]
    	public EntityKey EntityKey
    	{
    		get
    		{
    			return _entityKey;
    		}
    		set
    		{
    			if (_entityKey != value)
    			{
    				_entityKey = value;
    				OnPropertyChanged("EntityKey");
    			}
    		}
    	}
    	private EntityKey _entityKey;
    <# 
    		region.End();
    	}
    

    Finally, to avoid having the state to be set to modified each time EntityKey is set (since this isn't modifying the entity) you need to modify the OnPropertyChanged for the entity class, this can be done by modifying the line at 352 from:

        if (ChangeTracker.State != ObjectState.Added && ChangeTracker.State != ObjectState.Deleted)
    

    to:

        if (propertyName.Equals("EntityKey") == false && ChangeTracker.State != ObjectState.Added && ChangeTracker.State != ObjectState.Deleted)
    

    With these changes you have IEntityWithKey implementation in your entities!



    --Rune
    Sunday, July 10, 2011 7:21 AM