ODATA对具有动态字段的实体的查询

发布于 2025-01-23 00:04:35 字数 1568 浏览 4 评论 0 原文

我在动态模型上使用 odata myObject 数据以两种不同的方式存储在数据库中 - 在主 myObject 表和键/值对的辅助表中。辅助表的允许字段名称存储在第三个表中。使用 Pivot ,我可以检索包含静态和动态字段的 myObject 项目的列表。如果我分配了构建器的 entitySet < /代码>到 MyObject 模型。但是,我无法使用任何动态字段使用 $ filter $ proceby ,因为它们在 myObject 模型上不存在。

我尝试使用 DynamicObject ExpliantOobject 并使用 MyObject ,但仍无法运行 $ filter $ orderby 在任何先前未定义的字段上。在每种情况下,代码都会在其击中控制器之前失败,该消息不会在分配给构建器的任何模型上存在该字段。由于我需要考虑不同的用户具有不同的字段,并且允许自动包含新字段,因此我不知道如何设置此字段。

startup.cs 配置中,我有以下内容:

services.AddControllers(options =>
{
    options.ModelBinderProviders.RemoveType<DateTimeModelBinderProvider>();
})
    .AddOData(opt => 
       opt.AddRouteComponents("odata", GetEdmModel())
           .Expand()
           .Select()
           .OrderBy()
           .Filter()
           .Count()
           .SkipToken()
     )
    .AddNewtonsoftJson();

getedmmodel 方法如下:

var builder = new ODataConventionModelBuilder();
builder.EntitySet<MyObject>("MyObjectController");
builder.EntityType<MyObject>().HasKey(k => k.Id);
return builder.GetEdmModel();

我不太熟悉设置 odata 。有没有一种方法可以使用可以动态更改的类使用 entitySet ?还是可以在 entityType 中定义某些内容可以考虑动态字段的方法?当用户调用控制器时,我可以获取这些字段的列表 - 在需要对 myObject Entity进行任何查询之前。

提前致谢!

I am using OData on a dynamic model. The MyObject data is stored in two different ways on the database - in a main MyObject table and in a secondary table of key/value pairs. The allowed field names for the secondary table are stored in a third table. Using a PIVOT, I can retrieve a list of MyObject items with both static and dynamic fields included. I am also able to use $top, $filter, and $orderby on any static fields if I assign the builder's EntitySet to the MyObject model. However, I am unable to use $filter or $orderby with any of the dynamic fields because they do not exist on the MyObject model.

I have tried creating a dynamic model using DynamicObject or ExpandoObject and using in place of MyObject, but am still unable to run $filter or $orderby on any fields that are not previously defined. In every case, the code fails before it hits the controller with a message that the field does not exist on whatever model is being assigned to the builder. Since I need to account for different users have different fields, as well as allowing new fields to be included automatically, I do not know how to set this up.

In the Startup.cs ConfigureServices, I have the following:

services.AddControllers(options =>
{
    options.ModelBinderProviders.RemoveType<DateTimeModelBinderProvider>();
})
    .AddOData(opt => 
       opt.AddRouteComponents("odata", GetEdmModel())
           .Expand()
           .Select()
           .OrderBy()
           .Filter()
           .Count()
           .SkipToken()
     )
    .AddNewtonsoftJson();

The GetEdmModel method is as follows:

var builder = new ODataConventionModelBuilder();
builder.EntitySet<MyObject>("MyObjectController");
builder.EntityType<MyObject>().HasKey(k => k.Id);
return builder.GetEdmModel();

I am not very familiar with setting up OData. Is there a way to use an EntitySet with a class that can be altered dynamically? Or is there a way to define something in the EntityType that can account for dynamic fields? I can get a list of these fields when the user calls the controller - before any queries need to be made on the MyObject entity.

Thanks in advance!

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

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

发布评论

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

评论(2

铃予 2025-01-30 00:04:35

我通过删除 get> get myObjectController 上的 get 方法来使其工作起作用。基于,此属性可防止恶意查询。看来它也阻止使用 $ filter $ orderby 中的任何其他动态字段。

我不知道删除此属性是否是一个好主意,因此我仍然对如何处理此问题的任何其他建议开放。

I managed to get this to work by removing the EnableQuery attribute on the Get method of MyObjectController. Based on the documentation, this attribute prevents malicious queries. It seems that it is also preventing using any additional dynamic fields in $filter and $orderby.

I don't know if it is a great idea to remove this attribute, so I am still open to any other suggestions on how to handle this issue.

菊凝晚露 2025-01-30 00:04:35

您可以看一下Odata的打开类型。如果您的实体是这样定义的,则可以使用普通 [EnableQuery] 属性查询您的动态属性。

You can take a look at OData's Open Types. If your entity is defined like that you can query your dynamic properties with the normal [EnableQuery] attribute.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文