0

I'm currently getting an object based on a string property like this:

return DbContext.Products.FirstOrDefault(p => p.Route == route);

This is working correctly but I now need to get its child objects (Benefits) that are related in the model like this:

public IList<Benefit> Benefits { get; set; }

How can I get this product and all its child benefits?

Something like this, but this doesn't work:

return DbContext.Products.Include("Benefits")FirstOrDefault(p => p.Route == route);

Thanks

DBoi
  • 627
  • 2
  • 17
  • 35

1 Answers1

1

Can you try like below:

return DbContext.Products.Include(x=>x.Benefits).FirstOrDefault(p => p.Route == route);

And in startup class:

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddMvc()
        .AddJsonOptions(
            options => options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        );

    ...
}
sri harsha
  • 676
  • 6
  • 16