Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Changes must ignore deleted items if not persisted #199

Open
noufionline opened this issue Nov 5, 2017 · 2 comments
Open

Merge Changes must ignore deleted items if not persisted #199

noufionline opened this issue Nov 5, 2017 · 2 comments
Assignees
Labels

Comments

@noufionline
Copy link

noufionline commented Nov 5, 2017

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.

var createdOrder = CreateOrder(client, newOrder, formatter);
            PrintOrderWithDetails(createdOrder);

            // Update the order
            Console.WriteLine("\nPress Enter to update order details");
            Console.ReadLine();

            // Start change-tracking the order
            var changeTracker = new ChangeTrackingCollection<Order>(createdOrder);

            // Modify order details
            createdOrder.OrderDetails[0].UnitPrice++;
            createdOrder.OrderDetails.RemoveAt(1);
            var newItem = new OrderDetail
            {
                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 changes
            var changedOrder = changeTracker.GetChanges().SingleOrDefault();
           // Once the GetChanges called OrderDetails is again populated with Deleted Item
            var updatedOrder = UpdateOrder(client, changedOrder, formatter);

            // Merge changes
            changeTracker.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 shown

            Console.WriteLine("Updated order:");
            PrintOrderWithDetails(createdOrder);
@tonysneed tonysneed self-assigned this Nov 8, 2017
@tonysneed tonysneed added the bug label Nov 8, 2017
@noufionline
Copy link
Author

Please find some time to fix this.

@cbgreely
Copy link

cbgreely commented Apr 9, 2018

Hi Tony,

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

        protected override void RemoveItem(int index)
        {
            // Mark existing item as deleted, stop listening for property changes,
            // then fire EntityChanged event, and cache item.
            if (Tracking)
            {
                // Get item by index
                TEntity item = Items[index];

                // Exclude this collection and Parent entity (used in M-M relationships)
                // from recusive algorithms: SetModifiedProperties, SetTracking and SetState.
                var visitationHelper = new ObjectVisitationHelper(Parent);
                visitationHelper.TryVisit(this);

                // Remove modified properties
                item.ModifiedProperties = null;
                item.SetModifiedProperties(null, visitationHelper.Clone());

                // Stop listening for property changes
                item.PropertyChanged -= OnPropertyChanged;

                // Disable tracking on trackable properties
                item.SetTracking(false, visitationHelper.Clone(), true);

                // Mark item and trackable collection properties
                item.SetState(TrackingState.Deleted, visitationHelper.Clone());

                // Fire EntityChanged event
                if (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 cached
                if (item.TrackingState != TrackingState.Added
                    && !_deletedEntities.Contains(item))
                    _deletedEntities.Add(item);
            }
            base.RemoveItem(index);
        }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants