Odata V7& 8-当键值绑定时,复合键是否应处于Alpha顺序或HASKEY?

发布于 2025-01-27 10:25:14 字数 1559 浏览 4 评论 0 原文

当使用键值绑定约定时,ODATA似乎想要alpha顺序的键。

下面的文件表明,它应该按照Haskeys定义的顺序,因为密钥不按alpha顺序为单位。

如果我下载 https://github.com/odata /aspnetcoreodata

将以下文件的第19行更改为“ haskey”定义中的firstName和lastname

切换人物模型文件中2个属性的顺序:

并更改以下文件第48行中的2个参数的顺序:

API仍期望第一个名称:

这是预期的行为吗?

除了向HTTPGET添加路由模板之外,我是否错过了确定密钥顺序的方法?

我更喜欢使用会议路由(HTTPGET中没有模板),我只是不确定是否有一种方法可以控制约定使用的键。

我正在将项目升级到.NET 6,ODATA 8,并且我试图将其移至使用会议路由而不是模板指定路由的过程中,但是目前的优先级是使路由保持不变。

与项目的链接是.NET 5,ODATA 7,但我已经证实了.NET 6和ODATA 8的同一问题。

Odata seems to want keys in alpha order when using key value binding convention.

The below document suggests it should be in the order defined in HasKeys, as the keys aren't in alpha order.
https://learn.microsoft.com/en-us/odata/webapi/key-value-binding

Yet if I download https://github.com/OData/AspNetCoreOData

Change line 19 of the below file to switch FirstName and LastName in the HasKey definition
https://github.com/OData/AspNetCoreOData/blob/main/sample/ODataRoutingSample/Models/EdmModelBuilder.cs

Switch the order of the 2 properties in the People Model file:
https://github.com/OData/AspNetCoreOData/blob/main/sample/ODataRoutingSample/Models/Person.cs

And change the order of the 2 parameters in line 48 of the below file:
https://github.com/OData/AspNetCoreOData/blob/main/sample/ODataRoutingSample/Controllers/PeopleController.cs

The API is still expecting the FirstName first:
enter image description here

Is this expected behaviour?

Have I missed a way to determine the order of the keys aside from adding a route template to the HttpGet?

I'd prefer use the convention route (no template in the HttpGet), I'm just not sure if there is a way to control the order of the keys the convention uses.

I'm in the process of upgrading a project to .Net 6, OData 8 and I'm trying to move it to using convention routes rather than template specified routes but the priority is, for the moment, to keep the routes the same.

The linked to project is .Net 5, OData 7 but I've confirmed the same issue with .Net 6 and OData 8.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

╄→承喏 2025-02-03 10:25:14

这是ODATA ModelBuilder中的实现:

var keys = ((IEnumerable<PropertyConfiguration>)config.Keys)
                                     .Concat(config.EnumKeys)
                                     .OrderBy(p => p.Order)
                                     .ThenBy(p => p.Name)
                                     .Select(p => type.DeclaredProperties.OfType<IEdmStructuralProperty>().First(dp => dp.Name == p.Name));

它按“订单”属性订购,然后是属性名称。
因此,您可以为“订单”设置不同的值,以获取所需的订单。

  1. 设置“订单”属性值直接
  2. 在C#属性上

使用'columnAttribute'属性。更新:

请注意,在Odataroutingsample中, firstName lastName 是显式添加的(通过call haskey fluent api),因此,'coluthatatTribute'将为明确添加的属性忽略。

因此,您应该直接设置订单值。以下代码应起作用:

builder.EntitySet<Person>("People").EntityType.HasKey(c => new { c.FirstName, c.LastName });
builder.EntityType<Person>().Property(c => c.FirstName).Order = 2;
builder.EntityType<Person>().Property(c => c.LastName).Order = 1;

Here's the implementation in the OData modelbuilder:

var keys = ((IEnumerable<PropertyConfiguration>)config.Keys)
                                     .Concat(config.EnumKeys)
                                     .OrderBy(p => p.Order)
                                     .ThenBy(p => p.Name)
                                     .Select(p => type.DeclaredProperties.OfType<IEdmStructuralProperty>().First(dp => dp.Name == p.Name));

It orders by the 'Order' property, thenby the property name.
So, you can set different value for 'Order' to get the orders that you want.

  1. Set the 'Order' property value directly
  2. use 'ColumnAttribute' attribute on the C# property.

Updated:

Be noted, in ODataRoutingSample, FirstName and LastName are added explicitly (by call HasKey fluent API), So, 'ColumnAttribute' will be ignore for explicitly added property.

So, you should set the order value directly. The following codes should work:

builder.EntitySet<Person>("People").EntityType.HasKey(c => new { c.FirstName, c.LastName });
builder.EntityType<Person>().Property(c => c.FirstName).Order = 2;
builder.EntityType<Person>().Property(c => c.LastName).Order = 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文