c# - ASP.NET Web Api OData v4 - 如何通过 $metadata 反射(reflect)小数位数和精度?

标签 c# asp.net asp.net-web-api odata

使用 ASP.NET Web API 2.2/OData v4.0 是否可以在 $metadata 端点中定义小数位数和精度?

目前我已经定义了一个包含许多小数属性的 PricingModel.cs:

...
public decimal? PurchasePrice { get; set; }        
public decimal? SellingPrice { get; set; }
...

PricingController.cs继承自ODataController,并在Startup.cs中定义了一个基本的实体数据模型:

public void Configuration(IAppBuilder appBuilder)
{
    var config = new HttpConfiguration();

    // Model builder
    ODataModelBuilder builder = new ODataConventionModelBuilder();
    builder.EntitySet<PricingModel>("Pricing");

    // Map route (Default)
    config.MapODataServiceRoute(
        routeName: "ODataRoute",
        routePrefix: null,
        model: builder.GetEdmModel());

    appBuilder.UseWebApi(config);
}

$metadata 中反射(reflect)的是(缺少比例和精度):

...    
<Property Name="PurchasePrice" Type="Edm.Decimal"/>
<Property Name="SellingPrice" Type="Edm.Decimal"/>
...

OData Version 4.0 Common Schema Definition应该允许将比例和精度包含在 $metadata 中。

6.2.4 Attribute Scale

A decimal property MAY define a non-negative integer value or variable for the Scale attribute.

This attribute specifies the maximum number of digits allowed to the right of the decimal point.

The value variable means that the number of digits to the right of the decimal point may vary from zero to the value of the Precision attribute.

An integer value means that the number of digits to the right of the decimal point may vary from zero to the value of the Scale attribute, and the number of digits to the left of the decimal point may vary from one to the value of the Precision attribute minus the value of the Scale attribute. If Precision is equal to Scale, a single zero has to precede the decimal point.

The value of the Scale attribute MUST be less than or equal to the value of the Precision attribute. If no value is specified, the Scale facet defaults to zero.

Note: if the underlying data store allows negative scale, services may use a Precision attribute with the absolute value of the negative scale added to the actual number of significant decimal digits, and client-provided values may have to be rounded before being stored.

Example 9: Precision and Scale facets applied to the Decimal type. Allowed values: 1.23, 0.23, 3.14 and 0.7, not allowed values: 123, 12.3.

<Property Name="Amount" Type="Edm.Decimal" Precision="3" Scale="2" />

Example 10: Precision equals Scale. Allowed values: 0.23, 0,7, not allowed values: 1.23, 1.2.

<Property Name="Amount" Type="Edm.Decimal" Precision="2" Scale="2" />

Example 11: Precision and a variable Scale applied to the Decimal type. Allowed values: 0.123, 1.23, 0.23, 0.7, 123 and 12.3, not allowed would be: 12.34, 1234 and 123.4 due to the limited precision.

<Property Name="Amount" Type="Edm.Decimal" Precision="3" Scale="variable" />

阅读Configure the OData Endpoint我知道我可能应该通过 ODataModelBuilder 实例以某种方式修改实体数据模型 (EDM),但似乎找不到解决方法。

An EDM is an abstract model of the data. The EDM is used to create the service metadata document. The ODataConventionModelBuilder class creates an EDM by using default naming conventions. This approach requires the least code. If you want more control over the EDM, you can use the ODataModelBuilder class to create the EDM by adding properties, keys, and navigation properties explicitly.

最佳答案

下面的代码成功了,也许对其他人有帮助......

...
// Model builder
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<PricingModel>("Pricing");
builder.EntitySet<PricingModel>("Pricing").EntityType.Property(x => x.PurchasePrice).Precision = 19;
builder.EntitySet<PricingModel>("Pricing").EntityType.Property(x => x.PurchasePrice).Scale = 3;
...

这就是现在 $metadata 中反射(reflect)的内容...

...
<Property Name="PurchasePrice" Type="Edm.Decimal" Precision="19" Scale="3"/>
...

关于c# - ASP.NET Web Api OData v4 - 如何通过 $metadata 反射(reflect)小数位数和精度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50157652/

相关文章:

c# - 带有源代码的 Monodroid 示例/小部件

JavaScript 与 WebBrowser 控件 C#

c# - 我们可以根据在 C# 中调用函数的位置来更改函数行为吗?

asp.net - 带有 JSON 集合的 Mustache JS 模板

asp.net - 如何使用Web API属性路由传递DateTime参数?

asp.net - 从url下载图像到服务器以在asp.net 5 api中处理

c# - 如何为游戏编译 C# 脚本

c# - WebService GET/POST 调用和 SOAP

asp.net - css文件更新版本的最佳解决方案是什么

c# - 如何阻止 EF 尝试更新 SQL Server 的计算列?