WCF 数据服务和预测。如何从实体投影实例获取实体 URI?
我正在使用 DataServiceContext 加载一些实体投影(实体有许多属性,为了最大限度地减少流量,我只加载目前需要的那些属性),如下所示:
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
};
我需要的是实体的有效 URI 来加载它以获取详细信息看法。
我尝试使用 ctx.TryGetUri(obj, out uri),但它总是返回 null (可能是因为非跟踪投影,但是,我已经加载了 PK 属性(Id),所以它一定不是案件)。
问题是,如何确定具有 PK 的投影对象的基础数据实体的 URI?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 C# 中,匿名类型是使用不可设置的属性生成的(这些属性没有设置器)。因此,WCF 数据服务客户端无法跟踪这些(因为它没有任何意义,它无法在具体化期间覆盖属性值)。所以结果是实例没有被跟踪。
要解决此问题,只需声明一个具有所需属性的非匿名类,然后投影到该类中(确保属性是可设置的)。
请注意,VB 的匿名类型确实具有可设置的属性,因此它们将被跟踪。
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.