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?