首页 > 解决方案 > 在 OData 中匹配大小写属性时出现 AmbiguousMatchException

问题描述

我有两个名称相同但大小写不同的属性,Title并且TITLE

public class Product
{
    [Key]
    public Guid Id { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }

    [NotMapped]
    public virtual string Title { get; set; }

    public string TITLE { get; set; }
}

我在 OData 配置中包含标题:

ODataModelBuilder builder = new ODataConventionModelBuilder();
        builder.EntitySet<Product>("Products");
        builder.EntityType<Product>().Property(a => a.Title);
        config.MapODataServiceRoute(
        routeName: "ODataRoute",
        routePrefix: null,
        model: builder.GetEdmModel());

这是 OData 控制器的操作:

  public IHttpActionResult Get(ODataQueryOptions<Product> queryOptions, CancellationToken cancellationToken)
  {
     Context = GetContext();
     var products = Context.GetEntities<Product>();
     var result = queryOptions.ApplyTo(products);
     return Ok(result);
  }

当我发送https://localhost:44326/Products?$select=Id,TITLE请求时,queryOptions.ApplyTo(products);我得到以下异常:

System.Reflection.AmbiguousMatchException:“找到不明确的匹配项。”

我想使用 $select 获取 Title 和 TITLE 属性。有谁知道如何解决这个问题?

标签: c#odata

解决方案


这是 OData 的问题。此问题将在 7.3 版本中修复。这是拉取请求: https ://github.com/OData/WebApi/pull/1907


推荐阅读