我是 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_描述 |
|---|---|---|
| 1 | 玉米 | 空值 |
pei_pests 表:
| p_id | 姓名 | 金銀絲 |
|---|---|---|
| 1 | 害蟲1 | 空值 |
| 2 | 害蟲2 | 空值 |
pei_cropspes 表:
| p_id | c_id |
|---|---|
| 1 | 1 |
| 2 | 1 |
現在在我的 API 中,我想展示類似的東西
[
{
"cId":1,
"pests":[
{
"pId":1,
"pName": pest1,
"pURL": null
},
{
"pId":2,
"pName": pest2,
"pURL": null
}
]
}
]
到目前為止,我的 get 請求在 C# web API 專案中如下所示:
[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; }
}
}
uj5u.com熱心網友回復:
你很接近,但你也沒有像你那樣完全使用 EF,我的意思是你實際上不必自己制作關系表,但可以直接參考物體 pei_crop 中的物體 pei_pests 串列并讓 EF創造另一個。
//Example just getting one property from each,
//but you can new a composite return type up if you wish, using select
var cropDetails = await _db.PeiCrops
.Where(c=>c.CId == cropId)
.Include(i=>i.PeiCropspests)
.ThenInclucde(t => t.Pests)
.Select(s => new { CropId = s.p_id, PestName = s.PeiCropsPests.Pest.p_name })
.ToListAsync();
https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select?view=net-5.0
uj5u.com熱心網友回復:
首先,您需要配置關系:
class MyContext : DbContext
{
...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<PeiCropspest>()
.HasKey(cp => new { cp.PId, cp.CId });
//Configure one PeiPest to many PeiCropspest
modelBuilder.Entity<PeiCropspest>()
// Specify PeiCropspest's navigation property to one PeiPest
.HasOne(cp => cp.PIdNavigation)
// Specify PeiPest's navigaton property to many PeiCropspest
.WithMany(p => p.PeiCropspests)
// Specify PeiCropspest's navigation property
// to use this PeiCropspest's property as foreign key
.HasForeignKey(cp => cp.PId);
//Configure one PeiCrop to many PeiCropspest
modelBuilder.Entity<PeiCropspest>()
// Specify PeiCropspest's navigation shadow property to one PeiCrop
.HasOne<PeiCrop>()
// Specify PeiCrop's navigaton property to many PeiCropspest
.WithMany(c => c.PeiCropspests)
// Specify PeiCropspest's navigation shadow property
// to use this PeiCropspest's property as foreign key
.HasForeignKey(cp => cp.CId);
}
public DbSet<PeiCrop> PeiCrops { get; set; }
}
然后你可以在 LINQ 查詢中做一個投影:
public async Task<IActionResult> GetSpecific(int cropId)
{
var cropDetails = await _db.PeiCrops
.Where(c=>c.CId == cropId)
.Select(c => new {
cId = c.CId,
pests = c.PeiCropspests.Select(p => new {
pId = p.PIdNavigation.PId,
pName = p.PIdNavigation.PName,
pUrl = p.PIdNavigation.PPhotoUrl
})
})
.ToListAsync();
return Ok(cropDetails);
}
你知道嗎?從 EF Core 5 開始,可以在沒有中間物體的情況下建立多對多關系。這可以簡化您的物體模型。參見 檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/361967.html
標籤:C# mysql sql-server 实体框架 asp.net-web-api
下一篇:如何重新排列串列索引
