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

(Client Side Question) Merge fresh Order from DB in a ChangeTrackingCollection<Order> #154

Open
Blastnsmash opened this issue Mar 8, 2017 · 9 comments
Labels

Comments

@Blastnsmash
Copy link

What is the proper way to update a ChangeTrackingCollection of Order when you want to merge a "fresh" Order that is fetched from the DB but you want to retain the same index of the existing order in the ChangeTrackingCollection.

var _order1 = //fetch from db
ChangeTrackingCollection _Orders = new ChangeTrackingCollection(order1 );

//Detect stale data, grab new value
var _neworder1 = //fetch from db;
_Orders[0] = _neworder1;

In the above approach tracking is turned off for the _neworder1 entity. You can do the following to turn it back on:

_Orders.Tracking = false;
_Orders.Tracking = true;

Is there a better way to do this? This approach would reset any pending changes you had on the client.

Is there something like a MergeFresh() that I'm overlooking? This method would take a "Fresh" Order and update tracking appropriately. For example, an Order has a Freight and Discount Property. Client Side both are changed from 1,1 to 2,2 respectively. Client detects there are changes from another user. Client finds that Freight has changed but Discount has not changed values on the DB are currently 3,1. Client MergeFresh() is called. It changes the state of Freight to Unchanged with a value of 3. It leaves the trackable state of Discount to Modified with a value of 2 because the Original value matches the DB value.

@tonysneed
Copy link
Collaborator

Have you tried the MergeChanges extension method for ChangeTrackingCollection<TEntity>? See MergeChanges in the source.

For an example, see the Program.Main in the Console client for any of the samples.

Does this address your question?

@Blastnsmash
Copy link
Author

Blastnsmash commented Mar 10, 2017

I'm trying to reproduce the scenario where in a multi-user environment another user updates data.

   // TODO: Address for Web API service (replace port number)
        const string serviceBaseAddress = "http://localhost:" + "58527" + "/";
        var client = new HttpClient { BaseAddress = new Uri(serviceBaseAddress) };

        var _order =  GetOrder(client, 1);

        ChangeTrackingCollection<Order> _orders = new ChangeTrackingCollection<Order>(_order);

//Go to sql server and add or delete a child relationship. The refresh order will be the current unstale version.
var _refreshorder = GetOrder(client, 1);

        _orders.MergeChanges(_refreshorder);

//This does not refresh the Order in the list of _orders. _orders[0] still has 2 OrderDetails, while _refreshOrder has 3.

@tonysneed
Copy link
Collaborator

So here is now MergeChanges works.

Assuming that _refreshOrder is the un-stale version and that _orders is the stale version, MergeChanges will update _orders from _refreshOrder, so that the two will then match. I will try to reproduce what you're seeing. It may be a couple days, as I am currently traveling on vacation with my family and may have limited opportunity to do this. But I intend to get to it as soon as I can.

@Blastnsmash
Copy link
Author

Ok, that's how I assumed mergechanges() worked but it wasn't refreshing the ChangeCollection. I was using your samples to produce the issue. If I have time Monday I'll give more details.

Enjoy the weekend.

@Blastnsmash
Copy link
Author

Here is a screen cast, let me know if that helps clarify.
https://drive.google.com/file/d/0B-M6Yes83t08anFVcC1xOHVPa1U/view?usp=sharing

@tonysneed
Copy link
Collaborator

Terrific, I'll have a look.

@tonysneed
Copy link
Collaborator

Great use of screen capture to demo an issue. I'll reproduce it on my end and have a look at what's going on.

@Blastnsmash
Copy link
Author

Blastnsmash commented Mar 14, 2017

That's great, I'm glad that clarified the issue. Hopefully it's not just something I've coded incorrectly - I try to be mindful of people's time :)

@tonysneed
Copy link
Collaborator

Added a failing test for this in PR #156.

@tonysneed tonysneed added bug and removed question labels Mar 15, 2017
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

2 participants