我有像這樣的簡單控制器(第一個代碼)并且它工作得很好,但是當我添加 .Include 以包含與其他表的關系時,它會立即停止返回正確的 ObjectResult (盡管填充了數據)。郵遞員返回:無法得到任何響應 連接到http://localhost:51111/data/test 時出錯。為什么會發生這種情況:服務器無法發送響應:確保后端工作正常 自簽名 SSL 證書被阻止:通過在“設置”>“常規代理配置不正確”中關閉“SSL 證書驗證”來解決此問題 確保在“設置”>“代理請求超時”中正確配置了代理:在“設置”>“常規”中更改請求超時沒有異常發生,也沒有字段為空。public IActionResult test(){ var user = _context.UsersTable .SingleOrDefault(p => p.Id.ToString().Length > 0); return new ObjectResult(user); }public IActionResult test(){ var user = _context.UsersTable .Include(x => x.Items) .SingleOrDefault(p => p.Id.ToString().Length > 0); return new ObjectResult(user); // data IS in the user, even relational collection is filled with data, but it just does not return. }public class User{ [Key] public Guid Id { get; set; } public virtual ICollection<Item> Items { get; set; } = new HashSet<Items>(); protected User() { } public User(string login, string password) { (...) }}public class Item{ [Key] public Guid Id { get; set; } public string ItemName { get; set; } // Relation to User public Guid UserId { get; set; } public virtual User User { get; set; } protected Item() { } public Item(string name, User user) { Id = Guid.NewGuid(); UserId = user.Id; ItemName = name; }}public class AppContext : DbContext{ public AppContext(DbContextOptions<AppContext> options) : base(options) { Database.SetCommandTimeout(20000); } public DbSet<User> UsersTable { get; set; } public DbSet<Item> ItemsTable{ get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { }}到目前為止,我嘗試過:user.Items = user.Items.ToList();
1 回答

皈依舞
TA貢獻1851條經驗 獲得超3個贊
var options = new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
};
return Json(documents, options);
- 1 回答
- 0 關注
- 239 瀏覽
添加回答
舉報
0/150
提交
取消