OData v4展开语法为RETURN ERROR&QOOT;.属性'ProductType'不能在$EXPAND查询选项中使用。&QOOT; [英] OData v4 expand syntax is returns error "...The property 'ProductType' cannot be used in the $expand query option."

查看:37
本文介绍了OData v4展开语法为RETURN ERROR&QOOT;.属性'ProductType'不能在$EXPAND查询选项中使用。&QOOT;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

"不能在$Expand查询中使用属性‘ProductType’ 选项。"

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

下面是表、控制器和WebApiConfig的布局。我还可以在哪里查找关联失败的原因?

// 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 我在每种情况下都会得到相同的错误。 如果还需要其他东西来确定原因,我很乐意去查,我只需要知道到哪里去找就行了。

谢谢,Mike

推荐答案

我在Odata GitHub问题的帖子中找到了答案。issuecomment-248168536根据此评论,这是Microsoft.AspNet.OData包版本6.0.0中的新行为,是一个突破性的变化。 我返回到另一个正在运行的服务,签出了Nuget包,它安装的是5.6.0版,而不是最新的(目前是7.1.0版)。

因此,修复方法是在映射之前添加所需功能的配置行.

config.Expand().Select();
config.MapODataServiceRoute("odata", null, builder.GetEdmModel());
这是修复程序,用于全局启用该选项,使其与<;V6.0版本一样工作。该线程(13-01-modelbound-attribute)中引用了一个文档,该文档演示了通过模型属性对选项进行精细控制的新功能。

HTH,Mike

这篇关于OData v4展开语法为RETURN ERROR&QOOT;.属性&#39;ProductType&#39;不能在$EXPAND查询选项中使用。&QOOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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