Print Page | Close Window

EntityManager.RejectChanges() throw "Cannot call BeginEdit() on Deleted or Detached entities"

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2009
Forum Discription: For .NET 3.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=1159
Printed Date: 29-Apr-2025 at 11:02pm


Topic: EntityManager.RejectChanges() throw "Cannot call BeginEdit() on Deleted or Detached entities"
Posted By: afrizal.chen
Subject: EntityManager.RejectChanges() throw "Cannot call BeginEdit() on Deleted or Detached entities"
Date Posted: 20-Mar-2009 at 3:38am
Hi,
I'm creating 5 new entities that are related one to another, when I call EntityManager.RejectChanges() it throws InvalidOperationException {"Cannot call BeginEdit() on Deleted or Detached entities"}.
Here is the scenario:
1. EntityManager.FindEntities(EntityState.Added) <-- 5 entities (No Deleted, No Modified)
2. EntityManager.RejectChanges(), InvalidOperationException is thrown.
3. EntityManager.FindEntities(EntityState.Added) <-- 4 entities (No Deleted, No Modified)
    Here, the one entity that is gone is the FIRST entity that was created.
4. EntityManager.RejectChanges(), everything is fine.


These are the call stacks from the exception:
   at IdeaBlade.EntityModel.v4.Entity.System.ComponentModel.IEditableObject.BeginEdit()
   at System.Windows.Forms.CurrencyManager.OnCurrentChanged(EventArgs e)
   at System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32 newPosition, Boolean validating, Boolean endCurrentEdit, Boolean firePositionChange, Boolean pullData)
   at System.Windows.Forms.CurrencyManager.List_ListChanged(Object sender, ListChangedEventArgs e)
   at System.Windows.Forms.BindingSource.OnListChanged(ListChangedEventArgs e)
   at System.Windows.Forms.BindingSource.InnerList_ListChanged(Object sender, ListChangedEventArgs e)
   at System.ComponentModel.BindingList`1.OnListChanged(ListChangedEventArgs e)
   at System.ComponentModel.BindingList`1.FireListChanged(ListChangedType type, Int32 index)
   at System.ComponentModel.BindingList`1.Child_PropertyChanged(Object sender, PropertyChangedEventArgs e)
   at System.ComponentModel.PropertyChangedEventHandler.Invoke(Object sender, PropertyChangedEventArgs e)
   at IdeaBlade.EntityModel.v4.Entity.OnPropertyChanged(PropertyChangedEventArgs e)
   at IdeaBlade.EntityModel.v4.EntityGroup.OnEntityChanged(EntityChangedEventArgs e)
   at IdeaBlade.EntityModel.v4.EntityGroup.RemoveEntity(Entity entity)
   at IdeaBlade.EntityModel.v4.Entity.RejectChanges()
   at IdeaBlade.EntityModel.v4.EntityGroup.<RejectChanges>b__9(Entity e)
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at IdeaBlade.EntityModel.v4.EntityGroup.RejectChanges()
   at IdeaBlade.EntityModel.v4.EntityCache.<RejectChanges>b__8(EntityGroup t)
   at IdeaBlade.Util.v4.ExtensionFns.ForEach[T](IEnumerable`1 items, Action`1 action)
   at IdeaBlade.EntityModel.v4.EntityCache.RejectChanges()
   at IdeaBlade.EntityModel.v4.EntityManager.RejectChanges()
   at ....



Replies:
Posted By: kimj
Date Posted: 20-Mar-2009 at 2:16pm
I have been unable to reproduce the error you see.  In my tests, a RejectChanges() call detaches all Added entities, therefore returning 0 records in the second FindEntities() call, and no exceptions are thrown.  The "BeginEdit()" error message is coming from a bound UI control, which is still bound to the BindingSource whose data has effectively disappeared.  Check your handlers for the EntityChanging, EntityChanged and PropertyChanged events to see if they're behaving as expected.  If the problem persists, please send a solution which reproduces the problem to support.


Posted By: afrizal.chen
Date Posted: 22-Mar-2009 at 8:19am
Hi,
Thanks for the hint.
I was using normal databinding using binding source, I modified it to use ControlBindingManager and it works fine now.
But I have another issue of using ControlBindingManager, When I bind it to DevExpress labelControl, it throws exception "could not locate a suitable binder for a LabelControl".
Well it works for System.Windows.Forms.Label, but It will looks different in my form.
Please advise. Thanks.


Posted By: davidklitzke
Date Posted: 04-Apr-2009 at 9:10am
For a control to work with the ControlBindingManager, it needs a DataBinder.  We wrote one for the Windows Forms label, but not for the DevExpress label.  To see how to write a Custom Data Binder for a control, see:
 
http://www.ideablade.com/forum/forum_posts.asp?TID=1159 - http://www.ideablade.com/forum/forum_posts.asp?TID=1159


Posted By: pucsoftware
Date Posted: 22-Apr-2009 at 11:18am
"For a control to work with the ControlBindingManager, it needs a DataBinder.  We wrote one for the Windows Forms label, but not for the DevExpress label.  To see how to write a Custom Data Binder for a control, see:
  http://www.ideablade.com/forum/forum_posts.asp?TID=1159 - http://www.ideablade.com/forum/forum_posts.asp?TID=1159 "
 
the above post refers to a link that describes how to write a custom data binder but the link isn't correct. It refers back to this post not a different one.


-------------
puctx


Posted By: kimj
Date Posted: 22-Apr-2009 at 11:54am
I think he meant to refer you here: http://www.ideablade.com/forum/forum_posts.asp?TID=133 - http://www.ideablade.com/forum/forum_posts.asp?TID=133



Print Page | Close Window