简体   繁体   English

如何使用导航属性更新Web API OData实体?

[英]How to update Web api OData Entity with a navigation property?

Entity1 has a collection of Entity2 as a navigation property. Entity1具有Entity2的集合作为导航属性。 I can successfully get Entity1 from ODataController with expanded collection of Entity2. 我可以使用扩展的Entity2集合从ODataController成功获取Entity1。 But when I try to update Entity1 using ODataController PUT or PATCH methods, I can do it only when a collection of Entity2 is null. 但是,当我尝试使用ODataController PUT或PATCH方法更新Entity1时,仅当Entity2的集合为null时,我才可以这样做。 Otherwise Delta is null. 否则,Delta为null。

public IHttpActionResult Put([FromODataUri] int key, Delta<ClienteleExportTemplate> delta)

What is interesting: when I use POST method of the same ODataController nested collection of Entity2 is filled with values and I can successfully save Entity1. 有趣的是:当我使用同一ODataController的POST方法时,Entity2的嵌套集合填充了值,因此我可以成功保存Entity1。 So the problem is only in PUT and PATCH methods. 因此,问题仅在于PUT和PATCH方法。

This is my modelBuilder config for this entities: 这是我针对该实体的modelBuilder配置:

modelBuilder.EntitySet<Entity1>("Entity1OData");
modelBuilder.EntitySet<Entity2>("Entity2OData");

I send values to POST and PUT methods as an JSON array using $odataresourse module for angularJs. 我使用$ odataresourse模块为angularJs将值作为JSON数组发送到POST和PUT方法。

How to configure Web api 2 ODatav4 to support navigation properties update? 如何配置Web API 2 ODatav4以支持导航属性更新?

Currently not support update entity relationship, As workaround, you can use the $ref to update the entity relationship. 当前不支持更新实体关系,作为解决方法,您可以使用$ ref来更新实体关系。 Please refer here for example: 请参考此处的示例:

https://github.com/xuzhg/WebApiSample/tree/eb795e26547555666410a79b88e3930d22479798/WebApiODataSample https://github.com/xuzhg/WebApiSample/tree/eb795e26547555666410a79b88e3930d22479798/WebApiODataSample

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM