如何将对象(实体)传递给OData操作?我可以传递简单类型和ComplexType,但是如果尝试传递EntityType,我会得到:
How can I pass an object (Entity) to an OData Action? I can pass simple types and ComplexType, but if try to pass an EntityType I get:
Invalid parameter type 'apitest.Models.Product'. A non-binding parameter type must be either Primitive, Complex, Collection of Primitive or a Collection of Complex. var modelBuilder = new ODataConventionModelBuilder(); modelBuilder.EntitySet<Product>("Products"); modelBuilder.EntitySet<PurchaseOrder>("PurchaseOrders"); var addProduct = modelBuilder.Entity<PurchaseOrder>().Action("AddProduct"); addProduct.Parameter<Product>("product"); addProduct.ReturnsFromEntitySet<PurchaseOrder>("PurchaseOrders");我尝试将addProduct.Parameter<Product>("product");替换为:
var productConfig = modelBuilder.StructuralTypes.OfType<EntityTypeConfiguration>().Single(x => x.Name == "Product"); addProduct.SetBindingParameter("product", productConfig, false);但是我还找不到任何有关如何使用SetBindingParameter的文档.如果我将Product注册为ComplexType,则第一个代码示例有效,但是之后我将其注册为Entity.
But I haven't been able to find any documentation on how to use SetBindingParameter. If I register Product as a ComplexType, the first code sample works, but then I'm not allowed to register it as Entity.
推荐答案假设您将操作绑定到实体类型,则可以使用
Assuming that you bind an action to an Entity type this works
builder.EntityType<company>().Action("SetCommunities").CollectionEntityParameter<community>("communities"); public async Task<IHttpActionResult> SetCommunities(Guid key, ODataActionParameters parameters)在这种情况下,例如 localhost:63358/odata/公司(451735ef-15f2-4a9f-b4b9-d50c6410bf7c)/Default.SetCommunities/
更多推荐
如何使用ASP.NET Web Api将对象作为参数传递给OData Action?
发布评论