【发布时间】:2021-12-24 23:52:36
【问题描述】:
我是 C# 实体框架的新手。我正在尝试构建 API,但卡在从关系表中检索数据。
我在 MS SQL 数据库中有一个 pei_crops 表,其中 c_id 是主键。我有另一个名为 pei_pests 的表,其中 p_id 是主键。另一个表是 pei_cropspests,我在其中建立了针对哪种害虫袭击哪种作物的关系。多种害虫可以攻击一种作物,一种害虫可以攻击多种作物。在这个 pei_cropspests 表中,我将 p_id 作为主键和外键,将 c_id 作为主键和外键.
pei_crops 表:
| c_id | c_name | c_description |
|---|---|---|
| 1 | Corn | NULL |
pei_pests 表:
| p_id | p_name | p_URL |
|---|---|---|
| 1 | pest1 | NULL |
| 2 | pest2 | NULL |
pei_cropspests 表:
| p_id | c_id |
|---|---|
| 1 | 1 |
| 2 | 1 |
现在我想在我的 API 中展示类似的东西
[
{
"cId":1,
"pests":[
{
"pId":1,
"pName": pest1,
"pURL": null
},
{
"pId":2,
"pName": pest2,
"pURL": null
}
]
}
]
到目前为止,我在 C# Web API 项目中的 get 请求如下所示:
[Route("Getspecific/{cropId}")]
[HttpGet]
public async Task<IActionResult> GetSpecific(int cropId)
{
var cropDetails = await _db.PeiCrops.Where(c=>c.CId == cropId).Include(i=>i.PeiCropspests).ToListAsync();
return Ok(cropDetails);
}
此代码仅返回影响 cID 编号 1 的害虫的 pID 和 URL。但我还想要害虫名称和 URL 以及它们的 id。
谁能告诉我怎么做。也许有一些方法可以连接两个表并显示数据?我只是不知道如何在 C# 中做到这一点。任何帮助表示赞赏。谢谢。
实体类: PeiCrop:
using System;
using System.Collections.Generic;
#nullable disable
namespace PEI_API.EF
{
public partial class PeiCrop
{
public PeiCrop()
{
PeiCropimages = new HashSet<PeiCropimage>();
PeiCropsdiseases = new HashSet<PeiCropsdisease>();
PeiCropspests = new HashSet<PeiCropspest>();
}
public int CId { get; set; }
public string CName { get; set; }
public string CPhotoUrl { get; set; }
public string CDescription { get; set; }
public virtual ICollection<PeiCropimage> PeiCropimages { get; set; }
public virtual ICollection<PeiCropsdisease> PeiCropsdiseases { get; set; }
public virtual ICollection<PeiCropspest> PeiCropspests { get; set; }
}
}
佩佩斯:
using System;
using System.Collections.Generic;
#nullable disable
namespace PEI_API.EF
{
public partial class PeiPest
{
public PeiPest()
{
PeiCropspests = new HashSet<PeiCropspest>();
PeiPestimages = new HashSet<PeiPestimage>();
}
public int PId { get; set; }
public string PName { get; set; }
public string PPhotoUrl { get; set; }
public string PDescription { get; set; }
public virtual ICollection<PeiCropspest> PeiCropspests { get; set; }
public virtual ICollection<PeiPestimage> PeiPestimages { get; set; }
}
}
PeiCropspest:
using System.Collections.Generic;
#nullable disable
namespace PEI_API.EF
{
public partial class PeiCropspest
{
public int PId { get; set; }
public int CId { get; set; }
public virtual PeiCrop CIdNavigation { get; set; }
public virtual PeiPest PIdNavigation { get; set; }
}
}
【问题讨论】:
-
也许LINQ可以做到?
-
@urlreader 我很困惑,不知道如何将它与我当前的代码集成并将其返回给 API。
-
你能展示实体类吗?
-
@Sami 使用此站点学习如何询问 SQL,对现在和将来都非常有用:meta.stackoverflow.com/questions/333952/…
-
@LeandroBardelli 感谢您的指出。我已经修好了
标签: c# mysql sql-server entity-framework asp.net-web-api