Print Page | Close Window

Entity State Change before entity add to manager

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2165
Printed Date: 10-Jun-2026 at 8:46pm


Topic: Entity State Change before entity add to manager
Posted By: bstearns
Subject: Entity State Change before entity add to manager
Date Posted: 16-Sep-2010 at 11:22am
Hello Everyone 
I am seeing something with my entity manager when adding a new entity that doesn't make sense to me.
From all my reading it seems that when I call 
 
X foo = EntityManger.CreateEntity<X>(); 
 
I will get a new instance of the entity of type X and its entity state will be detatched until I explicity call EntityManager.AddEntity(foo).
 
This is exactly how it works if my type X does not have property of another type on it.  The propblem seems to happen when I have a type X that has a property of type Y in it.  Then when I set the Type Y property the entity state then changes to added and if I look at the EntityManager.Xs I see the entity already attached there.
 
To try and illustrate this better here is my code:

TypeX foo= EntMan.CreateEntity<TypeX >();
 
//at this point foo has an entitystate = detatched and EntMan.TypeXs does not contain anything
foo.FooID = Guid.NewGuid();
foo.SomeProp1 = "Something";
            
//bar is a type that is already in cache and has an entity state = unchanged
foo.SomePropThatIsOfOtherTypeInCache = bar;
//after setting SomePropThatIsOfOtherTypeInCache we can see that
//foo.entitystate = added and EntMan.TypeXs now conatins the foo entity.
 
At this point because the entitystate is added and the entitymanager already contains the entity there is no reason to call EntityManager.AddEntity(foo).
 
I cannot find any documentation that mentions this functionality.  Is this by design or do I not understand how this is suppose to work.
 
Thanks
Blaine



Replies:
Posted By: DenisK
Date Posted: 16-Sep-2010 at 2:21pm
Hi Blaine;

Yes, what you are observing is by design. An entity is automatically added if any of its related entities are already in the EntityManager.



Print Page | Close Window