1

when performing a query of table B including the data from table A, the entity within the other is obtained several times

[HttpGet("{id}")]
public ActionResult GetById(int id)
{
  using(var db=new ExampleContext())
  {
    var data = db.TableBs.Include(x => x.TableA).FirstOrDefault(x => x.Id == id);
    return Ok(data);
  }
}

Image behavior

when returning you get the error: System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed...

Models

public partial class TableA
{
    public int Id { get; set; }
    public string? Item { get; set; }
    public virtual TableB? TableB { get; set; }
}
public partial class TableB
{
    public int Id { get; set; }
    public string? Title { get; set; }
    public virtual TableA TableA { get; set; } = null!;
}

Tables DB

create table TableA(
Id int primary key identity(1,1),
Item varchar(10)  
);
create table TableB(
Id int primary key,
Title varchar(10)
constraint FK_TableA_TableB foreign key (Id) references TableA(Id)
);

0 Answers0