You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a user adds a record and then changes his mind and deletes it, ChangeTracker should not keep a reference to this item in the tracker.
Please see the code snippet extracted from your demo.
varcreatedOrder=CreateOrder(client,newOrder,formatter);PrintOrderWithDetails(createdOrder);// Update the orderConsole.WriteLine("\nPress Enter to update order details");Console.ReadLine();// Start change-tracking the ordervarchangeTracker=newChangeTrackingCollection<Order>(createdOrder);// Modify order detailscreatedOrder.OrderDetails[0].UnitPrice++;createdOrder.OrderDetails.RemoveAt(1);varnewItem=newOrderDetail{OrderId=createdOrder.OrderId,ProductId=3,Quantity=15,UnitPrice=30};createdOrder.OrderDetails.Add(newItem);//Problem occures after this deletion which is not persisted yet in the database.createdOrder.OrderDetails.Remove(newItem);// At this point neither change tracker deleted collection nor the orderDetails collection has the deleted item.// Submit changesvarchangedOrder=changeTracker.GetChanges().SingleOrDefault();// Once the GetChanges called OrderDetails is again populated with Deleted ItemvarupdatedOrder=UpdateOrder(client,changedOrder,formatter);// Merge changeschangeTracker.MergeChanges(updatedOrder);// After merge changes only 2 records must be available but its shows 3. The deleted item which is not persisted in the database is still shownConsole.WriteLine("Updated order:");PrintOrderWithDetails(createdOrder);
The text was updated successfully, but these errors were encountered:
I am hitting this issue also. In debugging the code, it appears that ChangeTrackingCollection.RemoveItem is adding the item to the _deletedEntities collection even though it was newly added. Does the TrackingState need to be set to deleted AFTER deciding whether or not to add it to _deletedEntities? See my code snippet below. Thanks
protectedoverridevoidRemoveItem(intindex){// Mark existing item as deleted, stop listening for property changes,// then fire EntityChanged event, and cache item.if(Tracking){// Get item by indexTEntityitem=Items[index];// Exclude this collection and Parent entity (used in M-M relationships)// from recusive algorithms: SetModifiedProperties, SetTracking and SetState.varvisitationHelper=newObjectVisitationHelper(Parent);visitationHelper.TryVisit(this);// Remove modified propertiesitem.ModifiedProperties=null;item.SetModifiedProperties(null,visitationHelper.Clone());// Stop listening for property changesitem.PropertyChanged-=OnPropertyChanged;// Disable tracking on trackable propertiesitem.SetTracking(false,visitationHelper.Clone(),true);// Mark item and trackable collection propertiesitem.SetState(TrackingState.Deleted,visitationHelper.Clone());// Fire EntityChanged eventif(EntityChanged!=null)EntityChanged(this,EventArgs.Empty);//SHOULD THE FOLLOWING STATEMENT GO ABOVE "item.SetState(TrackingState.Deleted, visitationHelper.Clone());" ??// Cache deleted item if not added or already cachedif(item.TrackingState!=TrackingState.Added&&!_deletedEntities.Contains(item))_deletedEntities.Add(item);}base.RemoveItem(index);}
Hi Tony,
I am facing an issue with the change tracker.
If a user adds a record and then changes his mind and deletes it, ChangeTracker should not keep a reference to this item in the tracker.
Please see the code snippet extracted from your demo.
The text was updated successfully, but these errors were encountered: