OData v4 expand syntax is returns error quot;...The property #39;ProductType#39; cannot be used in the $expand query option.quot;(OData v4展开语法为RETURN ERRORQOOT;.属性#39;ProductType#39;不能在$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;.属性';ProductType';不能在$EXPAND查询选项中使用。&QOOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:OData v4展开语法为RETURN ERROR&QOOT;.属性';ProductType';不能在$EXPAND查询选项中使用。&QOOT;
				
        
 
            
        - C#MongoDB使用Builders查找派生对象 2022-09-04
 - MoreLinq maxBy vs LINQ max + where 2022-01-01
 - 如何用自己压缩一个 IEnumerable 2022-01-01
 - 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
 - WebMatrix WebSecurity PasswordSalt 2022-01-01
 - 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01
 - 输入按键事件处理程序 2022-01-01
 - 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
 - Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
 - C# 中多线程网络服务器的模式 2022-01-01
 
						
						
						
						
						
				
				
				
				