我在动态模型上使用 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!
发布评论
评论(2)
我通过删除
get> get
myObjectController
上的get
方法来使其工作起作用。基于,此属性可防止恶意查询。看来它也阻止使用$ filter
和$ orderby
中的任何其他动态字段。我不知道删除此属性是否是一个好主意,因此我仍然对如何处理此问题的任何其他建议开放。
I managed to get this to work by removing the
EnableQuery
attribute on theGet
method ofMyObjectController
. 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.
您可以看一下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.