我正在嘗試通過使用代碼優(yōu)先方法生成數(shù)據(jù)庫(kù)來構(gòu)建標(biāo)準(zhǔn) REST API 來學(xué)習(xí) Entity Framework Core。我在從當(dāng)前端點(diǎn)返回的 JSON 中獲取了空值GET,可以幫助找出出錯(cuò)的地方。這些控制器當(dāng)前是 Entity Framework Core 生成的默認(rèn)支架控制器。GET問題是我安裝時(shí)返回的 JSON是:{ "installId": 1, "aadUser": "Mr. Doe", "progress": 0, "practice": null //Shouldn't this reference a specific Practice?}這是一對(duì)一的關(guān)系。型號(hào)類別: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);
API 模型中的空引用
翻翻過去那場(chǎng)雪
2023-06-25 14:32:07