我正在嘗試通過使用代碼優(yōu)先方法生成數(shù)據庫來構建標準 REST API 來學習 Entity Framework Core。我在從當前端點返回的 JSON 中獲取了空值GET,可以幫助找出出錯的地方。這些控制器當前是 Entity Framework Core 生成的默認支架控制器。GET問題是我安裝時返回的 JSON是:{ "installId": 1, "aadUser": "Mr. Doe", "progress": 0, "practice": null //Shouldn't this reference a specific Practice?}這是一對一的關系。型號類別:public class Install{ public long InstallId { get; set; } public string AADUser { get; set; } public int Progress { get; set; } public Practice Practice { get; set; }}public class Practice{ public long PracticeId { get; set; } public string Name { get; set; } public string Specialty { get; set; } public long InstallId { get; set; } public Install Install { get; set; } public ICollection<User> Users { get; set; } public ICollection<User_Access> Users_Access { get; set; } public ICollection<Billing> Billing { get; set; } public ICollection<Location> Locations { get; set; } public ICollection<Resource> Resources { get; set; }}上下文片段:protected override void OnModelCreating(ModelBuilder modelBuilder){ //Install modelBuilder.Entity<Install>(entity => { entity.HasKey(e => e.InstallId); entity.HasOne(d => d.Practice) .WithOne(p => p.Install) .OnDelete(DeleteBehavior.Cascade); //Seed Database entity.HasData(new Install() { InstallId = 1, AADUser = "Mr. Doe", Progress = 0 }); }); //Practice modelBuilder.Entity<Practice>(entity => { entity.HasKey(e => e.PracticeId); entity.HasOne(d => d.Install) .WithOne(p => p.Practice) .OnDelete(DeleteBehavior.ClientSetNull);
1 回答

守著星空守著你
TA貢獻1799條經驗 獲得超8個贊
EF Core 不會自動包含相關對象。
您需要將其包含在您的查詢中
await?_context.Installs.Include(install?=>?install.Practice).ToListAsync();
- 1 回答
- 0 關注
- 131 瀏覽
添加回答
舉報
0/150
提交
取消