This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
Hi, It seems there is a problem when adding a child object and simultaneously editing another child object. Both objects are child objects of the same parent.To recreate use the NWIND Order object, add an order detail, and modify an already existing order detail.I am receiving the following exception:Type: InvalidOperationException Message: Collection was modified; enumeration operation may not execute. Data: 0 entries Stack trace: at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.List`1.Enumerator.MoveNext() at Mindscape.LightSpeed.Mapping.ObjectUnloader.VisitEntity(Entity entity, EntityChangeSet changeSet, Set`1 visitedSet) in H:\Storage\Developer\ORM\MindScape.LightSpeed\Mapping\ObjectUnloader.cs:line 224 at Mindscape.LightSpeed.Mapping.ObjectUnloader.VisitEntities(IEnumerable`1 entities) in H:\Storage\Developer\ORM\MindScape.LightSpeed\Mapping\ObjectUnloader.cs:line 182 at Mindscape.LightSpeed.Mapping.ObjectUnloader.PerformUnload(IEnumerable`1 entities) in H:\Storage\Developer\ORM\MindScape.LightSpeed\Mapping\ObjectUnloader.cs:line 30 at Mindscape.LightSpeed.UnitOfWork.SaveChanges(Boolean reset) in H:\Storage\Developer\ORM\MindScape.LightSpeed\UnitOfWork.cs:line 178 at Mindscape.LightSpeed.Repository.SaveChanges(Boolean resetUnitOfWork) in H:\Storage\Developer\ORM\MindScape.LightSpeed\Repository.cs:line 361 at Mindscape.LightSpeed.Repository.SaveChanges() in H:\Storage\Developer\ORM\MindScape.LightSpeed\Repository.cs:line 346 at Mindscape.LightSpeed.Repository.CompleteUnitOfWork(Boolean saveChanges) in H:\Storage\Developer\ORM\MindScape.LightSpeed\Repository.cs:line 395 at Mindscape.LightSpeed.Repository.CompleteUnitOfWork() in H:\Storage\Developer\ORM\MindScape.LightSpeed\Repository.cs:line 380 at JSEF.Core.ObjectSpace.IntCommitChanges() at JSEF.Core.ObjectSpace.CommitChanges() at JSEF.Core.Win.SystemModule.WinDetailViewController.ExecuteSaveAndClose(SimpleActionExecuteEventArgs args) in H:\Storage\Developer\JSEF2\JSEF.Core.Win\SystemModule\WinDetailViewController.cs:line 89 at JSEF.Core.SystemModule.DetailViewController.f_sa_SaveAndClose_OnExecute(Object sender, SimpleActionExecuteEventArgs e) InnerException is null Note the exception does not occur if only adding a new detail item or just modifying an existing detail item, however, it also does occur when removing a detail item and also modifiying.Thanks in advance, Kavan
|
|
|
Hi Kavan, I'm having trouble duplicating this. Is there anything else going on in your model at save time that could be causing this? Cheers, Andrew. employee.BirthDate = DateTime.Now; employee.HireDate = DateTime.Now; employee.FirstName = "Foo"; employee.LastName = "Foo"; Shipper shipper = new Shipper(); shipper.CompanyName = "Baz"; Supplier supplier = new Supplier(); supplier.CompanyName = "Foo"; Product product = new Product(); product.ProductName = "Bar"; product.CategoryId = new Guid("9625b890-5317-48bf-b806-0a4a0e6dac8d"); product.Supplier = supplier; OrderDetail orderDetail = new OrderDetail(); orderDetail.UnitPrice = 123.45M; orderDetail.Quantity = 23; orderDetail.Product = product; Order order = new Order(); order.OrderDate = DateTime.Now; order.RequiredDate = DateTime.Now; order.ShippedDate = DateTime.Now; order.Employee = employee; order.ShipVia = shipper; order.Details.Add(orderDetail); Customer customer = new Customer(); customer.CustomerID = "Foo"; customer.CompanyName = "Bar"; customer.Orders.Add(order); Repository.Add(order); Repository.SaveChanges(true); order = Repository.FindOne<Order>(order.Id); Assert.IsNotNull(order); order.Details[0].Discount = 0.32; OrderDetail orderDetail2 = new OrderDetail(); orderDetail2.UnitPrice = 456.78M; orderDetail2.Quantity = 32; orderDetail2.Product = product; order.Details.Add(orderDetail2); Repository.SaveChanges();
|
|
|
Hi Andrew, It seems that in response to a property changed event I was making a list change. However, just to note error did completly go away when I modified ObjectUnloader.cs line 224 from foreach (Entity childEntity in entityCollection)
to:
foreach (Entity childEntity in new System.Collections.ArrayList(entityCollection))
What would be a downside with making a list copy first? Another solution would be to expose a suppress property change field so that the object loader/unloader could prevent this kind of error from happening. Thanks for the quick response, Kavan |
|
|
Where are you catching the property changed event during the save? I'm just keen to fully understand the scenario before I go ahead and make a fix. Cheers, Andrew. |
|
|
Hi Andrew, The problem was actually a bit more involved. I went per your recommendation and started using Repository instead of IUnitOfWorkFactory and holding a ref to the UOF. What started happening was I had objects loaded and then on subsequent finds objects were being loaded anew. So I had in memory to copies of the same object. This was because calling CompleteUnitOfWork on Repository causes the current UOF to be disposed and any object maps with it. So to resolve it I had setup a refresh system that would monitor RegisterEntity on UOF. It was a bad idea. But know I got rid of that setup and reverted to holding a ref to a UOF so that I can ensure only one copy is in mem at any one time. Hope this explains a bit. Essentially this problem is directly related to me having to implement a multi-level undo and change tracking system. I would love to hear what you think. Thanks, Kavan |
|