我正在尝试通过使用代码优先方法生成数据库来构建标准 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 关注
- 105 浏览
添加回答
举报
0/150
提交
取消