亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法以 ObjectResult 的形式返回具有關系的對象

無法以 ObjectResult 的形式返回具有關系的對象

C#
搖曳的薔薇 2021-10-31 19:04:57
我有像這樣的簡單控制器(第一個代碼)并且它工作得很好,但是當我添加 .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);


查看完整回答
反對 回復 2021-10-31
  • 1 回答
  • 0 關注
  • 239 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號