WCF数据服务和预测。如何从实体投影实例中获取实体URI? [英] WCF Data Services and Projections. How to get the entity URI from an entity projection instance?

查看:53
本文介绍了WCF数据服务和预测。如何从实体投影实例中获取实体URI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用DataServiceContext来加载一些实体投影(实体具有许多属性,以最小化流量,我只加载那些当前需要的属性),如下所示:

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
       
};


我需要的是实体的有效URI,以便为详细信息视图加载它。

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

我试图使用ctx.TryGetUri (obj,out uri),但它总是返回null(可能是因为非跟踪投影,但是,我已经加载了PK属性(Id),所以情况一定不是这样)。

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).

问题是,如何确定具有PK的投影对象的基础数据实体的URI?

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

推荐答案

您好,

您可以通过调用ctx.GetEntityDescriptor(entity)来检查是否正在跟踪实体。如果它返回null,则不跟踪实体。

You can check if the entity is being tracked by calling ctx.GetEntityDescriptor(entity). If it returns null, the entity is not being tracked.

上面的示例将永远不会跟踪实体,因为在C#中,匿名类型具有只读属性(属性没有setter为他们生成)。我们无法跟踪具有只读属性的实体,因为我们无法在从GET响应中读取数据时修改它们的值
。(例如)。

The above sample will never track the entity because in C#, anonymous types have read-only properties (the properties don't have setters generated for them). We can't track entities with read-only properties, cause we would not be able to modify the values of them when reading the data from a GET response (for example).

在这种情况下,您需要提供非匿名类型。最简单的方法是使用要投影的属性定义一个类,然后在查询中实例化该类。如果它具有来自真实实体类的关键属性,则应该跟踪它。

You need to provide a non-anonymous type in this case. The simplest way is to define a class with the properties you want to project and then instantiate that class in your query. If it has the key properties from the real entity class, it should get tracked.

谢谢,


这篇关于WCF数据服务和预测。如何从实体投影实例中获取实体URI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆