Chad Schouggins
Chad Schouggins

Reputation: 3521

WCF Odata .Expand Caching

If I call an OData service containing Vendors and Products, I can query Products calling .Expand("Vendor") which will populate the .Vendor property of each Product.

The issue I'm facing is if I query Products without expanding Vendor then later run another query on Products attempting to expand Vendor, any Products that are cached from the previous query does not get populated.

So a simple demonstration,

var products = client.Products.Expand("Vendor").ToList();

Will return all products with the Vendor property populated as expected.

But after

var products1 = client.Products.ToList();
var products2 = client.Products.Expand("Vendor").ToList();

products2 will not have Vendor populated, presumably because these Product objects are already cached, so WCF doesn't bother investigating further but just returns what it has.

I can run client.LoadProperty() on each one, but this is a pain and would kill performance. I can create a new DataServiceContext, but this feels wrong (although I can't seem to find anything explicityly suggesting the lifespan of one of these objects).

What's the best way to go here?

Upvotes: 1

Views: 306

Answers (1)

Yi Ding - MSFT
Yi Ding - MSFT

Reputation: 3024

Add a such line before getting any product:

client.MergeOption = MergeOption.OverwriteChanges;

And it will work as you expect.

Upvotes: 2

Related Questions