New Posts New Posts RSS Feed: Inconsistencies saving entities
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Inconsistencies saving entities

 Post Reply Post Reply
Author
c63205 View Drop Down
Newbie
Newbie


Joined: 28-Dec-2010
Location: Houston, TX
Posts: 10
Post Options Post Options   Quote c63205 Quote  Post ReplyReply Direct Link To This Post Topic: Inconsistencies saving entities
    Posted: 21-Mar-2011 at 10:08am
I'd appreciate any guidance with this issue.  It involves creating entities and having inconsistent entity states: Detached and Added.  Also, sometimes the new entities are not persisted to the DB.  Oddly, this behavior only seems to have occurred recently.
 
I've just recently started developing with devforce and so far, I create my entities with the code:

MasterQuestion mq = appUtil.Manager.CreateEntity<MasterQuestion>();

It appeared that sometimes the new entity would have the 'Added' state, and at other places an entity would be in the 'Detached' state.
 
So far, I have NOT also included the AddEntity() line when creating new entities like so:  ...is this correct?  Why would I have been experiencing inconsistent results.
 

MasterQuestion mq = appUtil.Manager.CreateEntity<MasterQuestion>();

mq.ID = Guid.NewGuid();

appUtil.Manager.AddEntity(mq);

Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 21-Mar-2011 at 1:34pm
Hi c63205,
 
When you create a new entity its EntityState is Detached.
 
If you 'link' this new Detached entity to an entity already attached to an EntityManager, (i.e. Added/Modified/Unchanged) the new entity will be automatically added to the EntityManager.
 
i.e.
var employee = mgr.Employees.First();
 
Order newOrder = mgr.CreateEntity<Order>(); // newOrder.EntityAspect.EntityState == EntityState.Detached
 
Order.Employee = employee; // newOrder.EntityAspect.EntityState == EntityState.Added
 
 
Note that the above will NOT happen if both entities are not attached to an EntityManager.
 
 
I suggest to always explicitly add the entity to an EntityManager.
 
Regards,
   Silvio.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down