I have configured OData in my application and able to perform basic operations on the WeatherForecast model. However, When I try to get to query on Address property, it giving below exception in #1 and #2 approaches, and in #3 approach, I am able to query on Address property.
Exception Message: "The query specified in the URI is not valid. It's not allowed to nest query options within 'Addresses' selection."
What's the difference EdmModel approach and the non-Edm model approach? It is mandatory to have id property on the model to register in Edm. Also, it is giving error when the model doesn't have an id property.
$exception {"The entity set 'WeatherForecast' is based on type 'WeatherAPI.WeatherForecast' that has no keys defined."} System.InvalidOperationException
Approaches
#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();
}
Model Classes
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; }
}