Print Page | Close Window

Inconsistencies saving entities

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=2572
Printed Date: 26-Apr-2026 at 9:23pm


Topic: Inconsistencies saving entities
Posted By: c63205
Subject: Inconsistencies saving entities
Date 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);




Replies:
Posted By: sbelini
Date 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.



Print Page | Close Window