I created a generic repository
worked with Entity framework core
targeted .Net 5
I created a method that will receive a primary key
value of an object and it (method) should select the object and load received related entities.
I try:
public virtual TEntity GetById( object pkValue , params Expression<Func<TEntity , object>>[] relatedEntitiesToBeLoaded )
{
// Change DbContext tracking behavior to track all entities
Context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.TrackAll;
// Get one object using primary key
var resultObject = Context.Set<TEntity>().Find( pkValue );
// Load all selected objects from selected entities
foreach ( var entityToLoad in relatedEntitiesToBeLoaded )
{
Context.Entry( resultObject ).Reference( entityToLoad ).Load();
}
return resultObject;
}
When I use this method like the example it works fine:
_groupProfSubjectRepository.GetById( id, x => x.Group , x => x.Prof , x => x.SchoolSubject );
But when I try to load navigation properties of another navigation properties of an TEntity
like the example bellow :
_groupProfSubjectRepository.GetById( id, x => x.Group , x => x.Prof , x => x.SchoolSubject, x=>x.SchoolSubject.FormationYear );
I get this error :
ArgumentException: The expression 'x => x.SchoolSubject.FormationYear' is not a valid member access expression. The expression should represent a simple property or field access: 't => t.MyProperty'. (Parameter 'memberAccessExpression')
To show the code details my repository is a searated project free and open source you can see the code here
Please any help to fix this issue ?