使用 EF Power 工具,我創建了以下類:
類別.cs
public partial class Category
{
public Category()
{
Folders = new HashSet<Folder>();
Reports = new HashSet<Report>();
}
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public string CategoryDescription { get; set; }
public string ImagePath { get; set; }
public virtual ICollection<Folder> Folders { get; set; }
public virtual ICollection<Report> Reports { get; set; }
}
檔案夾.cs
public partial class Folder
{
public Folder()
{
Reports = new HashSet<Report>();
}
public int FolderId { get; set; }
public string FolderName { get; set; }
public int CategoryId { get; set; }
public string FolderDescription { get; set; }
public string FolderImagePath { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Report> Reports { get; set; }
}
基礎表使用 Categoryid 一對多鏈接。我相信 dbContext.cs 是根據表模式正確生成的。
dbContext.cs
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Category>(entity =>
{
entity.HasKey(e => e.CategoryId)
.IsClustered(false);
entity.ToTable("Category");
entity.Property(e => e.CategoryId)
.ValueGeneratedNever()
.HasColumnName("CategoryID");
entity.Property(e => e.CategoryDescription)
.HasMaxLength(300)
.IsUnicode(false);
entity.Property(e => e.CategoryName)
.HasMaxLength(100)
.IsUnicode(false);
entity.Property(e => e.ImagePath)
.HasMaxLength(250)
.IsUnicode(false);
} );
modelBuilder.Entity<Folder>(entity =>
{
entity.HasKey(e => e.FolderId)
.IsClustered(false);
entity.ToTable("Folder");
entity.Property(e => e.FolderId)
.ValueGeneratedNever()
.HasColumnName("FolderID");
entity.Property(e => e.CategoryId).HasColumnName("CategoryID");
entity.Property(e => e.FolderDescription)
.HasMaxLength(300)
.IsUnicode(false);
entity.Property(e => e.FolderImagePath)
.HasMaxLength(250)
.IsUnicode(false);
entity.Property(e => e.FolderName)
.HasMaxLength(100)
.IsUnicode(false);
entity.HasOne(d => d.Category)
.WithMany(p => p.Folders)
.HasForeignKey(d => d.CategoryId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("FK_Folder_01");
});
OnModelCreatingPartial(modelBuilder);
}
然后我在控制器中有以下內容:
[HttpGet]
public async Task<ActionResult<IEnumerable<Category>>> GetCategories()
{
return await _context.Categories.ToListAsync();
}
當我運行 WebAPI 時,我看到以下內容:
[ {“categoryId”:1,“categoryName”:“某個名稱”,“檔案夾”:[],“報告”:[] },{“categoryId”:2,“categoryName”:“另一個名稱”,“檔案夾": [], "報告": [] }, ]
如何獲取相關資料以填充到檔案夾陣列中?
我也試過這個:
public async Task<ActionResult<IEnumerable<Category>>> GetCategories()
{
var query = (from r in _context.Categories
from bu in _context.Folders
where r.CategoryId == bu.CategoryId
select r
).ToListAsync();
return await query;
uj5u.com熱心網友回復:
我懷疑您的應用程式與延遲加載有加載關系。
這意味著在您要求特別查看它們之前,不會加載指定模型上的每個關系。
在這種特定情況下,您可以告訴您的應用程式一次性加載您需要的所有資料。
[HttpGet]
public async Task<ActionResult<IEnumerable<Category>>> GetCategories()
{
// note the .Include will tell you application context to load the relation Folders by default
return await _context.Categories.Include(x => x.Folders).ToListAsync();
}
如果您想一路學習,這里有更多資訊,您可以默認加載您的關系
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/519413.html
標籤:C#实体框架asp.net-core-webapief-电动工具