【发布时间】:2020-02-13 08:03:09
【问题描述】:
我刚开始学习 ASP.Net Core,但在关系方面有些问题。
在我的 Beer 模型中,我有以下导航属性:
/* Navigation Properties */
public int BreweryID { get; set; }
public Brewery Brewery { get; set; }
在我的 Brewery 模型中,我有以下导航属性:
/* Navigation Properties */
public ICollection<Beer> Beers { get; set; }
我的 dbContext 如下所示:
modelBuiler.Entity<Beer>()
.HasOne(c => c.Brewery)
.WithMany(u => u.Beers)
.HasForeignKey(c => c.BreweryID)
.IsRequired()
.OnDelete(DeleteBehavior.Restrict);
在控制器中,我这样询问我的数据:
// GET: api/brewery
[HttpGet]
public async Task<ActionResult<IEnumerable<Brewery>>> GetBreweries()
{
return await _context.Breweries.ToListAsync();
}
现在,我的数据库中有一些测试数据。 当我向 Postman 发出 Get 请求时,啤酒厂没有显示任何相关的啤酒。
在 Postman 中,它看起来像这样:
//啤酒
{
"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
},
//啤酒厂
{
"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
}
有什么建议吗?
【问题讨论】:
标签: c# asp.net entity-framework linq-to-entities