【发布时间】:2019-05-08 08:12:17
【问题描述】:
我从 .netcore 的 API 开始,我有一个外键,当我进行查询时,值为 null
在那种情况下,我读到 include 是用来获取它的值的,但是当我使用它时,它并没有显示表的所有值,并且在断点中,我看到它具有径向效果并且它不会从那里经过
我的控制器
public class ComidasController : ControllerBase
{
private readonly testcoreContext _context;
public ComidasController(testcoreContext context)
{
_context = context;
}
// GET: api/Comidas
[HttpGet]
public List<Comida> GetComida()
{
var prueba =_context.Comida.ToList();
return prueba;
}
// GET: api/Comidas/5
[HttpGet("{id}")]
public async Task<ActionResult<Comida>> GetComida(int id)
{
var comida = await _context.Comida.FirstOrDefaultAsync(x=> x.Id==id);
if (comida == null)
{
return NotFound();
}
return comida;
}
我的课
{
public partial class Comida
{
public int Id { get; set; }
public string Name { get; set; }
public int? Type { get; set; }
public virtual Types TypeNavigation { get; set; }
}
}
public partial class Types
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Comida> Comida { get; set; }
}
【问题讨论】:
-
你是否尝试找到? var comida = await _context.Comida.FindAsync(id)
-
你的完整对象包含循环依赖。使用此类中首选的数据传输对象 (DTO)
标签: c# linq asp.net-core