2
I am studying . NET Core API and having problems with relationships, where I get the error whenever I try to list all "calibrations" of "equipment".
JsonException: A possible object cycle was detected which is not supported. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 32.
System.Text.Json.ThrowHelper.ThrowInvalidOperationException_SerializerCycleDetected(int maxDepth)
When I remove: ".Include(c => c.Calibration)" from control, I receive json with null "calibration".
What I want is to get all the calibrations (Calibrated and Valid) of each equipment.
public class Equipamento
{
public int Id { get; set; }
public string Nome { get; set; }
public int TipoEquipamentoId { get; set; }
public TipoEquipamento TipoEquipamento { get; set; }
public decimal Valor { get; set; }
public string NotaFiscal { get; set; }
public DateTime Entrada { get; set; }
public List<Calibracao> Calibracao { get; set; }
}
public class Calibracao
{
public int Id { get; set; }
public int EquipamentoId { get; set; }
public Equipamento Equipamento { get; set; }
public DateTime Calibrado { get; set; }
public DateTime Validade { get; set; }
}
public class TesteContext : DbContext
{
public TesteContext(DbContextOptions<TesteContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Calibracao>()
.HasOne(p => p.Equipamento)
.WithMany(b => b.Calibracao);
}
public DbSet<Equipamento> Equipamentos { get; set; }
public DbSet<TipoEquipamento> TiposEquipamento { get; set; }
public DbSet<Calibracao> Calibracoes { get; set; }
}
// GET: api/Equipamento
[HttpGet]
public async Task<ActionResult<IEnumerable<Equipamento>>> GetEquipamentos()
{
return await _context.Equipamentos
.Include(t => t.TipoEquipamento)
.Include(c => c.Calibracao)
.ToListAsync();
}
It worked perfectly. Now, can I bring the last or first "calibration" record of each "equipment"?
– ShBr
I would tell you to ask a new question about how to create this kind of "relationship" on Ef. In a short time you should have answers.
– tvdias