Balanjaneyulu K
Balanjaneyulu K

Reputation: 4324

OData configuration in .NET Core 3.1

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; }
}

Upvotes: 1

Views: 1186

Answers (1)

Pliyo
Pliyo

Reputation: 621

How does your DBContext file looks like?

Entity Framework needs to know which one is your key, and I'm guessing that's the part missing here. Although by default EF will pick the property Id, since it's a Guid we'll be safer specifying it.

Try to change the WeatherForecast model for something like:

public class WeatherForecast
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    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; }
}

If you want to know more, have a look at these answers:
- Generate Guid on Server Side,
- EF 6 guid as primary key: cannot insert the value null into column id

Upvotes: 2

Related Questions