【发布时间】:2020-08-22 08:48:29
【问题描述】:
我已经在我的应用程序中配置了 OData,并且能够对 WeatherForecast 模型执行基本操作。 但是,当我尝试查询 Address 属性时,它在 #1 和 #2 方法中给出以下异常,而在 #3 方法中,我可以查询 Address 属性。
异常消息:“URI 中指定的查询无效。不允许在“地址”选择中嵌套查询选项。”
EdmModel 方法和非 Edm 模型方法有什么区别?必须在模型上具有 id 属性才能在 Edm 中注册。此外,当模型没有 id 属性时也会出错。
$exception {"实体集 'WeatherForecast' 基于没有定义键的类型 'WeatherAPI.WeatherForecast'。"} System.InvalidOperationException
方法
#1
endpoints.EnableDependencyInjection();
endpoints.Select().Filter().OrderBy().Count().MaxTop(null);
endpoints.MapODataRoute("odata", null, GetEdmModel());
#2
var builder = new ODataConventionModelBuilder();
endpoints.EnableDependencyInjection();
endpoints.Select().Filter().OrderBy().Count().MaxTop(null);
endpoints.MapODataRoute("odata", null, builder.GetEdmModel());
#3
endpoints.Select().Filter().OrderBy().Count().MaxTop(null);
endpoints.EnableDependencyInjection(b =>
{
b.AddService(Microsoft.OData.ServiceLifetime.Singleton, typeof(IEdmModel), sp => GetEdmModel());
});
GetEdmModel
public static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<WeatherForecast>("WeatherForecast");
return builder.GetEdmModel();
}
模型类
public class WeatherForecast
{
public Guid Id { get; set; }
public DateTime Date { get; set; }
public int TemperatureC { get; set; }
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
public string Summary { get; set; }
public Address Addresses { get; set; }
}
public class Address
{
public string StreetId { get; set; }
public string StreetName { get; set; }
}
【问题讨论】:
标签: c# asp.net-core odata asp.net-core-webapi