Dotnet Core Web API returning empty list | Lazy loading do not work

.net-core asp.net-core-webapi c# entity-framework-core

Question

I have an endpoint in my API which returns all the data of a selected item. The item is a root object called Survey and it has a list of pages.

public partial class Surveys
{
    public Surveys()
    {
        Pages = new HashSet<Pages>();
    }

    public string Description { get; set; }
    public string Name { get; set; }
    public long Syear { get; set; }
    public long Quarter { get; set; }
    public ICollection<Pages> Pages { get; set; }
}

Model class for the Pages look like this.

public partial class Pages
{
    public Pages()
    {
        Elements = new HashSet<Elements>();
    }

    public string Id { get; set; }
    public long Number { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int? PageFlowId { get; set; }
    public bool NamedPage { get; set; }
    public bool? IsFirst { get; set; }
    public bool? IsLast { get; set; }
    public long? SurveyQuarter { get; set; }
    public long? SurveySyear { get; set; }

    public PagesFlows PageFlow { get; set; }
    public Surveys Survey { get; set; }
    public ICollection<Elements> Elements { get; set; }
}

But when I send the GET request is returning an empty list for Pages

[
    {
        "description": "Customer Satisfaction Survey",
        "name": "Customer Survey",
        "syear": 2019,
        "quarter": 1,
        "pages": []
    }
]

The database contains data. primary key of the table is a composite key(Syear, Quarter). My API looks like this.

public async Task<IActionResult> GetSurveys([FromRoute]long syear, long quarter)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        var surveys = await _context.Surveys.Include(s => s.Pages).SingleOrDefaultAsync(s => s.Syear == syear && s.Quarter == quarter);
        if (surveys == null)
        {
            return NotFound();
        }

        return Ok(surveys);
    }

I have been trying to figure this out for a week now. Any help will be highly appreciated, Thank you in advance.

1
1
10/23/2018 5:59:21 AM

Accepted Answer

I managed to get around the issue by doing the following. Removed .Include(s => s.Pages) from here,

var surveys = await _context.Surveys.Include(s => s.Pages).SingleOrDefaultAsync(s => s.Syear == syear && s.Quarter == quarter);

I have added virtual keyword to all the referenced classes.

public partial class Pages
{
    public Pages()
    {
        Elements = new HashSet<Elements>();
    }

    public string Id { get; set; }
    public long Number { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int? PageFlowId { get; set; }
    public bool NamedPage { get; set; }
    public bool? IsFirst { get; set; }
    public bool? IsLast { get; set; }
    public long? SurveyQuarter { get; set; }
    public long? SurveySyear { get; set; }

    public virtual PagesFlows PageFlow { get; set; }
    public virtual Surveys Survey { get; set; }
    public virtual ICollection<Elements> Elements { get; set; }
} 

Then I have installed Microsoft.EntityFrameworkCore.Proxies package fron NuGet as described here

And enabled Lazy Loading Proxies in Sturtup.cs in ConfigureServices method.

services.AddDbContext<MyDBContext>
                (options => options.UseLazyLoadingProxies().UseSqlServer(connection));
1
10/23/2018 5:58:35 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow