c# - OData v4 扩展语法返回错误 "...The property ' ProductType' 不能用于 $expand 查询选项。”

标签 c# odata

我有一个新的 OData V4 服务,我正在尝试运行它,但我看到了意外错误...

"The property 'ProductType' cannot be used in the $expand query option."

我在另一个 OData 服务中没有遇到这个问题,我一直在比较两者,但我找不到两个 WRT 模型中项目的设置和 WebApiConfig 之间的显着差异。我按照文章 create-an-odata-v4-endpoint 中列出的示例构建了它而另一个是使用脚手架向导创建的。

下面是表格、 Controller 和 WebApiConfig 的布局。我还能在哪里寻找关联失败背后的原因?

enter image description here

// Product.cs
public partial class Product
{
    public int ProductId { get; set; }
    public int ProductTypeId { get; set; }
    public string Size { get; set; }
    public string PartNo { get; set; }

    public virtual ProductType ProductType { get; set; }
}

// ProductType.cs
public partial class ProductType{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]    public     ProductType()
    {
        this.Products = new HashSet<Product>();
    }
    public int ProductTypeId { get; set; }
    public string ProductTypeName { get; set; }
    public string Image { get; set; }
    public string ProductDescription { get; set; }
    public string InstallInstructions { get; set; }
    public string DataSheet { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Product> Products { get; set; }
}

// WebApiConfig.cs
public static void Register(HttpConfiguration config)
{
    // Web API configuration and services

    // Web API routes
    config.MapHttpAttributeRoutes();

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

    config.SetTimeZoneInfo(TimeZoneInfo.Utc);

    ODataConventionModelBuilder builder = new ODataConventionModelBuilder();

    builder.EntitySet<Product>("Products");
    builder.EntityType<Product>().HasKey(entity => entity.ProductId);
    builder.EntityType<Product>().HasRequired(entity => entity.ProductType, (entity, targetEntity) => entity.ProductTypeId == targetEntity.ProductTypeId);
    builder.EntitySet<ProductType>("ProductTypes");
    builder.EntityType<ProductType>().HasKey(entity => entity.ProductTypeId);
    builder.EntityType<ProductType>().HasMany(entity => entity.Products);

    config.MapODataServiceRoute("odata", "odata", builder.GetEdmModel());

    config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

}

// ProductTypesController.cs
public class ProductTypesController : BaseController
{
    [EnableQuery]
    public IQueryable<ProductType> Get()
    {
        return db.ProductTypes;
    }

    [EnableQuery]
    public SingleResult<ProductType> Get([FromODataUri] int key)
    {
        IQueryable<ProductType> result = db.ProductTypes.Where(p => p.ProductTypeId.Equals(key));
        return SingleResult.Create(result);
    }
    ....
}

// ProductsController.cs
public class ProductsController : BaseController
{

    [EnableQuery]
    public IQueryable<Product> Get()
    {
        return db.Products;
    }

    [EnableQuery]
    public SingleResult<Product> Get([FromODataUri] int key)
    {
        IQueryable<Product> result = db.Products.Where(p => p.ProductId.Equals(key));
        return SingleResult.Create(result);
    }
    ...
}

我已经尝试了两个方向,通过单选和多选来引用相关项目(在地址栏中输入这些 URL): /ProductTypes?$expand=Products,以及 /ProductTypes(3)?$expand=Products,以及 /Products?$expand=ProductType,和 /Products(3)?$expand=ProductType 在每种情况下我都会遇到同样的错误。 如果需要其他东西来确定原因,我会很乐意查找,我只需要知道在哪里查找。

谢谢,迈克

最佳答案

我在 odata github 问题的帖子中找到了答案。 issuecomment-248168536根据此评论,这是 Microsoft.AspNet.OData 包版本 6.0.0 中的新行为,是一项重大更改。 我回到另一个正在运行的服务并检查了 Nuget 包,它安装了 v 5.6.0 而不是最新的(当前是 7.1.0)。

因此解决方法是在映射之前为您想要的功能添加配置行...

config.Expand().Select();
config.MapODataServiceRoute("odata", null, builder.GetEdmModel());

这是全局启用该选项以使其像 < v6.0 版本一样工作的修复程序。该线程 ( 13-01-modelbound-attribute ) 中引用了一个文档,该文档演示了通过模型属性对选项进行精细控制的新功能。

HTH,迈克

关于c# - OData v4 扩展语法返回错误 "...The property ' ProductType' 不能用于 $expand 查询选项。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55464298/

相关文章:

c# - Restful 网址。查看特定型号

c# - 在 Net Core 中使用 Microsoft Graph Api 将用户添加为组成员

c# - WebApi OData 4,用于在 Controller 中批量插入的第二个 POST 端点

c# - 带有 DateTimeOffset 的 OData 函数?范围

c# - OData $orderby 查询中的自然排序

.net - oData v4 简单来说什么是函数和 Action ?

c# - 为列表 c# 中的特定索引应用 lambda 表达式

c# - 如何在Unity3D中使用参数更新Tweener?

c# - 用于提取 CSS 类名和 ID 的正则表达式

c# - Mono 上奇怪的 WeakReference 行为