OData的只读属性 [英] OData read-only property

查看:313
本文介绍了OData的只读属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有OData的V4一个的WebAPI 2.2的应用程序。另外我使用EF 6.1。

I have a WebAPI 2.2 application with OData V4. Also I'm using EF 6.1.

在我的实体之一我有一个计算的属性:

In one of my entities I have a calculated property:

public class Person {
  public string FirstName { get; set; }
  public string LastName { get; set; }
  // Calculated Property - No setter
  public string FullName { 
    get {
      return FirstName + " " + LastName;
    }
} 

为了提供计算的财产给我的客户,我需要在OData的模型注册

In order to provide the calculated property to my clients, I need to register in the OData Model

    public static IEdmModel GetModel()
    {
        ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.Namespace = "NavigationServices";
        builder.EntityType<Person>;   
        builder.EntityType<Person>().Property(a => a.FullName); // Calculated Property
        ....

        return builder.GetEdmModel();
    }

所以,当我获得客户端我的数据,每个对象都有计算的属性。

So, when I obtain my data in the client side, every object has the Calculated property.

然而,当我尝试创建(POST)的新元素或更新(PUT)一个现有的,我的动作不承认元素,并生成一个错误,指出它没有找到适合设置法属性。

However, when I try to Create (POST) a new element or Update (PUT) a existing one, my action don't recognize the element and generates an error saying that it doesn't find the "set method" for the property.

我看了几个帖子关于OData的只读属性(显然不支持),但我没有找到一个方法来使用的OData与计算性能。

I read a couple of posts about read only properties in OData (apparently not supported) but I don't find a way to use OData with calculated properties.

如何克服这种情况,一些建议吗?

Some advice of how to overcome this situation?

推荐答案

现在有这样做的柔软的方式,也就是建立使用注释客户端和服务器之间的契约。

Now there is a soft way for doing this which is to build a contract between the client and server using annotations.

在V4的核心词汇标准,有一个这样的术语:

In the Core vocabulary of the V4 standard, there is a such term:

<Term Name="Computed" Type="Core.Tag" DefaultValue="true" AppliesTo="Property">
    <Annotation Term="Core.Description" String="A value for this property is generated on both insert and update"/>
</Term>

在网页API的OData,在WebConfig.cs,你写这样code这样的注释添加到您的属性:

In Web API OData, in WebConfig.cs, you write such code to add such annotation to your property:

ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
var model = builder.GetEdmModel() as EdmModel;
model.SetVocabularyAnnotation(
    new EdmAnnotation(model.EntityContainer.FindEntitySet("People").EntityType().FindProperty("FullName"),
    new EdmTerm("Org.OData.Core.V1", "Computed", EdmPrimitiveTypeKind.Boolean),
    new EdmBooleanConstant(true)));

然后在你的数据,它会是这个样子:

Then in your data, it'll look something like this:

<Annotations Target="V4Service.Models.Person/FullName">
    <Annotation Term="Org.OData.Core.V1.Computed" Bool="true"/>
</Annotations>

通过服务上面的步骤通告,关于全名属性实体是由服务来计算。然后在POST和PATCH请求控制器方法你可以有你自己忽略客户端的全名属性时发送的任何值的逻辑和计算你自己的。

Through the steps above the service advertises that the FullName property on Person entity is computed by the service. Then in the controller methods for POST and PATCH requests you can have you own logic of ignoring any value sent by the client for the FullName property and compute your own.

我不知道你正在使用的客户端。如果您使用的OData客户端.NET ,我们获取标注值的支持将是我们的下一个版本。如果你不介意使用 EdmLib 直接注解值检索的支持已经被添加。

I'm not sure which client you are using. If you're using OData Client for .NET, our support for getting annotation values will be in our next release. If you don't mind using EdmLib directly, the annotation value retrieval support has already been added.

这篇关于OData的只读属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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