将自定义导航属性添加到OData Web API Controller [英] Adding a custom navigation property to OData Web API Controller

查看:95
本文介绍了将自定义导航属性添加到OData Web API Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个OData v3 Web API项目.它使用实体框架代码优先模型.

I have an OData v3 Web API project. It uses an Entity Framework Code First model.

主要课程是优惠券.它有一个列表.实际上,这是子元素ItemRequirement和BasketRequirement的2元素集合.我想能够说:

The main class is Coupon. It has a List. What this really is, is a 2-element collection of subtypes ItemRequirement and BasketRequirement. I want to be able to say:

../odata/Coupons(5)/ItemRequirement

我无法使它正常工作.

首先,在EF类中,我已将ItemRequirement添加为[NotMapped]属性(因为该类已经具有基类的集合作为导航属性,而将其他两个添加为属性只会生成无关的表键并不必要地弄乱了数据库.代码优先"中的逐层表"按原样运行很好.)

First, in the EF class, I have added ItemRequirement as a [NotMapped] property (as the class already has a collection of the base class as a navigation property, and adding the other two as properties would just generate extraneous table keys and mess up the database unnecessarily. The Table-Per-Hierarchy in Code First is working great as is).

ODataConventionModelBuilder()没有将ItemRequirement用作导航属性

The ODataConventionModelBuilder() is not picking up ItemRequirement as a navigation property

我尝试添加它:

// GET odata/Coupons(5)/ItemRequirement
public ItemRequirement GetItemRequirement( [FromODataUri] decimal key)
{
   return db.Coupons.Where(m => m.CouponId == key).SelectMany(m => m.RedemptionPurchaseRequirements).OfType<ItemRequirement>().FirstOrDefault();
}

URI永远不会进入此代码.通过添加IODataRoutingConvention实现程序,我发现ODataPath设置为navigation/key/unresolved.

The URI will NEVER get into this code. I have found by adding an IODataRoutingConvention implementor that the ODataPath is set to navigation/key/unresolved.

我查看了此解决方案,但对我也没有帮助:

I looked at this solution and it didn't help me, either:

向ODataConventionModelBuilder添加支持自定义查询的Navigation属性 a>

我不知道问题是否出在继承,属性未在EF中映射的事实还是什么.

I don't know if the problem is the inheritance, the fact that the property is not mapped in EF, or what.

我还发现此操作失败并显示404:

I have also found that this fails with a 404:

oData/PurchaseRequirementsBases(5)/myNamespace.ItemRequirement

仅仅是什么巫术才有必要抽象化集合,以便OData使用者可以将ItemRequirement视为Coupon的有效属性?

Just what witchcraft is necessary to abstract the collection away so the OData consumer can see ItemRequirement as a valid property of Coupon?

推荐答案

您可以尝试显式添加导航属性吗?

Can you try adding the navigation property explicitly?

odataConventionModelBuilder().Entity<Coupon>().HasOptional(coupon => coupon.ItemRequirement)

这篇关于将自定义导航属性添加到OData Web API Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆