【发布时间】:2019-09-11 05:41:21
【问题描述】:
我有两个名称相同但大小写不同的属性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 属性。有谁知道如何解决这个问题?
【问题讨论】:
-
为什么你有两个大小写不同的属性?您是否考虑过不这样做?
-
您是否尝试在“Title”属性中添加 [DataMember(Name = "ProductTitle"]?
-
@metal,我试过了,但我仍然遇到这个异常
-
是什么使它模棱两可的是标题和标题
-
@metal,我明白这一点。在 c# 中,我们可以为不同的情况创建相同的属性。如果没有办法解决这个问题,我会考虑重命名这些属性