0

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 ?

Amit Joshi
  • 15,448
  • 21
  • 77
  • 141
JOCKH
  • 339
  • 4
  • 15
  • You should really consider switching from `Find` method and explicit loading to `FirstOrDefault` with eager loading. See https://stackoverflow.com/questions/40360512/findasync-and-include-linq-statements/40360633#40360633, also https://stackoverflow.com/questions/39434878/how-to-include-related-tables-in-dbset-find/39435987#39435987 – Ivan Stoev May 04 '21 at 11:42
  • As for explicit loading, see https://stackoverflow.com/questions/54675380/is-it-possible-to-load-nested-navigation-properties-using-entityentry-reference and https://stackoverflow.com/questions/63882266/how-to-access-chained-nested-property-in-ef-core-2-2/63882630#63882630, however it requires playing with the passed expressions and extracting and separating the "reference" parts in order to be able to chain `Reference` calls until you get to the desired level. – Ivan Stoev May 04 '21 at 11:50
  • Please can you provide me an example as an answer, and I really appreciate this for you brother cause this the second time you helping me – JOCKH May 04 '21 at 11:58
  • @IvanStoev Please can you provide me an example as answer bro – JOCKH May 04 '21 at 12:30
  • For the last example `x=>x.SchoolSubject.FormationYear` you need somehow to execute `Context.Entry(resultObject ).Reference(x => x.SchoolSubject).Query().Include(y => y.FormationYear).Load();`, but how exactly to do that with the way you are passing the expressions is tough (cannot write code at this moment, but it requires playing with `Expression`s) – Ivan Stoev May 04 '21 at 13:01

1 Answers1

0

According to this documentation https://github.com/dotnet/efcore/issues/1585

Nested object (complex type) is not supported.

Jason Chen
  • 136
  • 3