Search code examples
apientity-framework-coreodataasp.net-core-2.2

Odata controller returns not complete result. (EDM model)


scenario

I have some entity called Category defined in my EFcontext. It is clean POCO class. In EFContext OnModelCreating method I apply entity configuration. In my startup I configure Odata for my queries and build EDM model based on Conventional Builder. My controller inherited from ODataController, has ODataRoutePrefix, OdataRoute and EnableQuery attributes.

Problem

In my response i received:

{
"@odata.context": "https://localhost:44362/api/odata/$metadata#Categories",
"value": [

It is not mistake. It ends with '['. No error. httpstatus: 200

odata config

        app.UseMvc(options =>
        {
            options.EnableDependencyInjection();
            options.Select().Filter().OrderBy().Count().Expand().MaxTop(100).SkipToken();
            options.MapODataServiceRoute("odata", "api/odata", GetEdmModel());               
        });


        private static IEdmModel GetEdmModel()
    {
        ODataModelBuilder builder = new ODataConventionModelBuilder();
        var category = builder.EntitySet<Category>("Categories");
        category.EntityType.HasKey(c => c.Id);

        return builder.GetEdmModel();
    }

EF CategoryTypeConfiguration

    public void Configure(EntityTypeBuilder<Category> categoryConfiguration)
    {
        categoryConfiguration.ToTable("Categories", QueryEFContext.DefaultSchema);

        categoryConfiguration.HasKey(category => category.Id);

        categoryConfiguration.Property(category => category.Name).IsRequired();
        categoryConfiguration.Property(category => category.Color).IsRequired();
    }

Tried

It returns proper response when I delete line:

 modelBuilder.ApplyConfiguration(new CategoryTypeConfiguration());

Solution

  • I once encountered this bug. In my case it happened because an error occurred in Entity Framework but the service returned a 200 OK anyway.

    To see if this is the case in your application you could configure Visual Studio to break on all exceptions:

    • Click: Debug -> Windows -> Exception Settings (Ctrl + Alt + E)
    • Check: 'Common Language Runtime Exceptions'

    Then send your request again and see if any exceptions pop up.