I have four models auto generated by Entity Framework Core (2.1.4) (using scaffold) that are related between themselves.
public partial class Run
{
public int Id { get; set; }
public int RunCarId { get; set; }
public int RunExtraInfoId { get; set; }
...
public RunExtraInfo RunExtraInfo{ get; set; }
public RunCar RunCar{ get; set; }
public ICollection<Weighing> Weighings{ get; set; }
}
public partial class RunExtraInfo
{
public int Id { get; set; }
...
public ICollection<CorridaCt> Runs { get; set; }
}
public partial class RunCar
{
public int Id { get; set; }
...
public Run Run{ get; set; }
}
public partial class Weighing
{
public int Id { get; set; }
public int RunId { get; set; }
...
public Run Run{ get; set; }
}
But whenever I include all info I need on Run, all other relations are loaded to the entities, making it very recursive.
Example:
var runs = ContextSQL.Runs
.Include(c => c.RunExtraInfo)
.Include(c => c.RunCar)
.Include(c => c.Weighings)
.ToList();
return runs;
Generates:
[
{
id: 0,
...
runExtraInfo: {
id: 0,
...
runs: [
{
id: 0,
runExtraInfo: {id: 0 runs: [...]},
runCar: {id: 0, runs: [...]}
}
]
},
runcar: {
id: 0,
runs: [
{id: 0, runExtraInfo: [...], runs: [...]}
]
}
}
]
I thought entity was supposed to include the relations only if we included them explicitly with Include(), am I wrong?
How can I solve this problem?
[Script Ignore]on places where you want to prevent recursion.