1

I just started learning ASP.Net Core and I have some problems with the relations.

In my Beer model i have these Navigation properties:

/* Navigation Properties */
public int BreweryID { get; set; }
public Brewery Brewery { get; set; }

In my Brewery model i have these Navigation properties:

/* Navigation Properties */
public ICollection<Beer> Beers { get; set; }

My dbContext looks like this:

modelBuiler.Entity<Beer>()
            .HasOne(c => c.Brewery)
            .WithMany(u => u.Beers)
            .HasForeignKey(c => c.BreweryID)
            .IsRequired()
            .OnDelete(DeleteBehavior.Restrict);

In the controller, I ask my data like this:

// GET: api/brewery
    [HttpGet]
    public async Task<ActionResult<IEnumerable<Brewery>>> GetBreweries()
    {
        return await _context.Breweries.ToListAsync();
    }

Now, i have some test data in my database. When I do a Get Request with Postman, none of the related beers are shown with the brewery.

In Postman, it looks like this:

//Beers

{
    "id": 11,
    "name": "test",
    "brand": "test",
    "alcoholContent": 2,
    "platoScale": 4,
    "description": "test",
    "breweryID": 7,
    "brewery": null,
    "imageID": 3,
    "image": null,
    "foodpairings": null,
    "categoryID": 1,
    "category": null,
    "favorites": null
},
{
    "id": 12,
    "name": "test",
    "brand": "test",
    "alcoholContent": 4,
    "platoScale": 7,
    "description": "test",
    "breweryID": 7,
    "brewery": null,
    "imageID": 4,
    "image": null,
    "foodpairings": null,
    "categoryID": 1,
    "category": null,
    "favorites": null
},

//the brewery

 {
    "id": 7,
    "name": "Moortgat",
    "address": "test",
    "founder": "test",
    "foundationYear": "2008-01-10T00:00:00.123",
    "description": "test",
    "website": "test",
    "beers": null,
    "imageID": 3,
    "image": null
}

Are there any suggestions?

Shadow
  • 33,525
  • 10
  • 51
  • 64
yens
  • 11
  • 1

3 Answers3

0

The related entities for Beer are not included in the query. Please include the same _context.Breweries.Include(brewery=>brewery.Beers)

Dee P
  • 7
  • 5
  • Thanks @Dee P for your answer. When I do this, I get an exception response. **System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.** – yens Oct 16 '19 at 15:04
0

Try this.

return await _context.Breweries
    .Include(x => x.Beers)
    .ToListAsync();

https://learn.microsoft.com/en-us/ef/core/querying/related-data

mjung
  • 139
  • 7
  • Thanks for your answer. I tried this one before, but I get an error response in Postman. The error is: System.Text.Json.JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32. – yens Oct 16 '19 at 14:58
  • Assuming you're on Core 3.0, maybe these articles will help: [link1](https://stackoverflow.com/questions/57912012/net-core-3-upgrade-cors-and-jsoncycle-xmlhttprequest-error) [link2](https://github.com/dotnet/corefx/issues/38579) – mjung Oct 16 '19 at 15:07
0

The exact issue you are having now is specified in this thread

.Net Core 3 and EF Core 3 Include Problem (JsonException) [Solved]

Dee P
  • 7
  • 5