Cannot include relational data with Entity Framework
我正在尝试在我的公司模型中包含关系数据。当我不使用
1 2 3 4 5 6 7 8 | public async Task<IActionResult> Get(string with, string orderBy) { CustContext context = new CustContext(); var companies = context.Companies.Include(c => c.Stores).ToListAsync(); return Ok(companies); } |
我想以
有人知道什么地方进展不顺利?
编辑
调试时,我看到
我找到了解决方案。显然,我们需要明确告知框架不要继续包含(直到无穷远为止)。因此,必须将其添加到
1 2 3 4 | services.AddMvc().AddJsonOptions(options => { options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore; }); |