I have a many-to-many relation between parents and childs using a relation table cause those relations are not automatically supported in EF Core yet:
class Parent{
[Key]
public int Id{get;set;}
public List<ParentChild> ParentChilds{get;set;}
}
class Child{
[Key]
public int Id{get;set;}
public List<ParentChild> ParentChilds{get;set;}
}
class ParentChild{
public int ParentId{get;set;}
public Parent Parent{get;set;}
public int ChildId{get;set;}
public Child Child{get;set;}
}
For editing the parent, I need to get ALL of his childs. Seems like a job for Include()
var db = new MyDbContext();
var parentWithChilds = db.Parents.Include(p => p.ParentChilds)
.FirstOrDefault(p => p.Id == 1);
This gave me the list of ParentChild
istances. But the Child
entity of ParentChild
isn't loaded automatically, so I only have the Id of the child, but not the Child object itself which is needed for me. I found ThenInclude
which seems to be designed for such cases and from examples like this I did the following:
var parentWithChilds = db.Parents.Include(p => p.ParentChilds)
.ThenInclude(p => p.Select(x => x.Child))
.FirstOrDefault(p => p.Id == 1);
But it throws an exception:
The property expression 'p => {from ParentChild x in p select [x].Child => FirstOrDefault()}' is not valid. The expression should represent a property access: 't => t.MyProperty'.
So how can this be done? I would like to avoid unnecessary queries like fetching the entity manually this way:
user.ParentChilds.ForEach(pc => pc.Child = db.Childs.FirstOrDefault(x => x.Id == pc.ChildId));