Multiple Includes() in EF Core

后端 未结 8 2155
傲寒
傲寒 2020-12-08 08:07

I have an extension method that lets you generically include data in EF:

public static IQueryable IncludeMultiple(this IQueryable          


        
8条回答
  •  再見小時候
    2020-12-08 08:47

        public TEntity GetByIdLoadFull(string id, List navigatonProoperties)
        {
            if (id.isNullOrEmpty())
            {
                return null;
            }
    
            IQueryable query = dbSet;
    
            if (navigationProperties != null)
            {
                foreach (var navigationProperty in navigationProperties)
                {
                    query = query.Include(navigationProperty.Name);
                }
            }
    
            return query.SingleOrDefault(x => x.Id == id);
        }
    

    Here is a much simpler solution, idea is to cast the dbset to iqueryable and then recursively include properties

提交回复
热议问题