.net-4.0wcf-data-servicesastoria

WCF Data Services and Projections. How to get the entity URI from an entity projection instance?


I'm using DataServiceContext to load some entity projections (entities have many properties, to minimize traffic I load only those properties, which are needed at the moment) like this:

from x in ctx.Portfolios
       select new 
       {
         Id = x.Id,
         Name = x.Name,
         PortfolioName = x.PortfolioName,
         Description = x.Description,
         ValidFrom = x.ValidFrom,
         ValidUntil = x.ValidUntil
       };

What I need is a valid URI of the entity to load it for details view.

I've tried to use ctx.TryGetUri(obj, out uri), but it always returns null (probably because of the non-tracking projections, however, I've loaded the PK property (Id), so it must not be the case).

The question is, how do I determine the URI of the underlying data entity, having a projection object with PK?


Solution

  • In C# anonymous types are generated with non-settable properties (the properties don't have setters). As a result WCF Data Services client can't track those (since it would not make any sense, it could not overwrite the property value during materialization). So the result is that the instance is not tracked. To workaround this just declare a non-anonymous class with the properties you need and project into that (make sure the properties are settable). Note that VB's anonymous types do have settable properties, so they will get tracked.