locked
unit of work compiled query in ef core 3x RRS feed

  • Question

  • User-640622294 posted

    this code working in ef core 2 

     private static Func<TContext,int, Task<TEntity>> _getById =
               EF.CompileAsyncQuery((TContext context,int id) =>
                   context.Set<TEntity>()
                      .AsNoTracking()
                      .Where(s => s.Id == id)
                      .FirstOrDefault());
    
            private static Func<TContext, AsyncEnumerable<TEntity>> _getAll =
               EF.CompileAsyncQuery((TContext context) =>
    
                  context.Set<TEntity>().AsNoTracking()
                );

    but does not running in ef core 3 

    i want to use below method but it has error

       private static Func<ClinicContext, AsyncEnumerable<T>> _getAll =
                 EF.CompileAsyncQuery((ClinicContext context) => context.Set<T>().AsNoTracking());

    ------------------------------------------------------

    Cannot implicitly convert type 'System.Func<Core3.Models.ClinicContext, System.Collections.Generic.IAsyncEnumerable<T>>' to 'System.Func<Core3.Models.ClinicContext, AsyncEnumerable<T>>' [Core3]csharp(CS0029)

    Tuesday, August 25, 2020 3:59 PM

All replies

  • User711641945 posted

    Hi reza-2017,

    What about trying the following code:

    private static Func<ClinicContext, AsyncEnumerable<T>> _getAll =
        (Func<MyDbContext, AsyncEnumerable<T>>)EF.CompileAsyncQuery((ClinicContextcontext) => context.Set<T>().AsNoTracking());

    Best Regards,

    Rena

    Wednesday, August 26, 2020 6:38 AM
  • User1116182075 posted

    thank you But does not work again !

    Wednesday, August 26, 2020 1:34 PM
  • User1116182075 posted
    public class GenericRepository<T> : IGenericRepository<T> where T : class
        {
            private readonly IUnitOfWork _unitOfWork;
            private ClinicContext _context;
    
            public GenericRepository(ClinicContext context)
            {
                _context = context;
                _unitOfWork = new UnitOfWork(context);
            }
    
            private static Func<ClinicContext, AsyncEnumerable<T>> _getAll =
                (Func<ClinicContext, AsyncEnumerable<T>>)
                EF.CompileQuery<T>((context)
                    => context.Set<T>().AsNoTracking());
    
            public IQueryable<T> Query()
            {
                return _context.Set<T>().AsQueryable();
            }
    
            public ICollection<T> GetAll()
            {
                return _context.Set<T>().ToList();
            }
            
            
            public async Task<ICollection<T>> GetAllAsync()
            {
                //return await _getAll(_context).ToListAsync();
                return await _context.Set<T>().ToListAsync();
            }
    
            public IEnumerable<T> ExecWithStoreProcedure(string query)
            {
                return _context.Set<T>().FromSqlRaw(query).AsNoTracking().ToList<T>();
            }
    
            public async Task<IList<T>> ExecWithStoreProcedureAsync(string query)
            {
                return await _context.Set<T>().FromSqlRaw(query).ToListAsync();
            }
    
            public async Task<IList<T>> ExecWithStoreProcedureAsync(string query, params object[] parameters)
            {
                return await _context.Set<T>().FromSqlRaw(query, parameters).ToListAsync();
            }
            
            public T GetById(int? id)
            {
                if (id == null)
                {
                    throw new ArgumentException("null");
                }
                return _context.Set<T>().Find(id);
            }
    
            public async Task<T> GetByIdAsync(int? id)
            {
                if (id == null)
                {
                    throw new ArgumentException("null");
                }
                return await _context.Set<T>().FindAsync(id);
            }
    
            public T GetByUniqueId(string id)
            {
                return _context.Set<T>().Find(id);
            }
    
            public async Task<T> GetByUniqueIdAsync(string id)
            {
                return await _context.Set<T>().FindAsync(id);
            }
    
            public T Find(Expression<Func<T, bool>> match)
            {
                return _context.Set<T>().SingleOrDefault(match);
            }
    
            public async Task<T> FindAsync(Expression<Func<T, bool>> match)
            {
                return await _context.Set<T>().SingleOrDefaultAsync(match);
            }
    
            public ICollection<T> FindAll(Expression<Func<T, bool>> match)
            {
                return _context.Set<T>().Where(match).ToList();
            }
    
            public async Task<ICollection<T>> FindAllAsync(Expression<Func<T, bool>> match)
            {
                return await _context.Set<T>().Where(match).ToListAsync();
            }
    
            public T Add(T entity)
            {
                _context.Set<T>().Add(entity);
                _context.SaveChanges();
                return entity;
            }
    
            public async Task<T> AddAsync(T entity)
            {
                _context.Set<T>().Add(entity);
                await _unitOfWork.Commit();
                return entity;
            }
    
            public T Update(T updated)
            {
                if (updated == null)
                {
                    return null;
                }
    
                _context.Set<T>().Attach(updated);
                _context.Entry(updated).State = EntityState.Modified;
                _context.SaveChanges();
    
                return updated;
            }
    
            public async Task<T> UpdateAsync(T updated)
            {
                if (updated == null)
                {
                    return null;
                }
    
                _context.Set<T>().Attach(updated);
                _context.Entry(updated).State = EntityState.Modified;
                await _unitOfWork.Commit();
    
                return updated;
            }
    
            public void Delete(T t)
            {
                _context.Set<T>().Remove(t);
                _context.SaveChanges();
            }
    
            public async Task<int> DeleteAsync(T t)
            {
                _context.Set<T>().Remove(t);
                return await _unitOfWork.Commit();
            }
    
            public int Count()
            {
                return _context.Set<T>().Count();
            }
    
            public async Task<int> CountAsync()
            {
                return await _context.Set<T>().CountAsync();
            }
    
            public IEnumerable<T> Filter(Expression<Func<T, bool>> filter = null, Func<IQueryable<T>, IOrderedQueryable<T>> orderBy = null, string includeProperties = "", int? page = null,
                int? pageSize = null)
            {
                IQueryable<T> query = _context.Set<T>();
                if (filter != null)
                {
                    query = query.Where(filter);
                }
    
                if (orderBy != null)
                {
                    query = orderBy(query);
                }
    
                if (includeProperties != null)
                {
                    foreach (
                        var includeProperty in includeProperties.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        query = query.Include(includeProperty);
                    }
                }
    
                if (page != null && pageSize != null)
                {
                    query = query.Skip((page.Value - 1) * pageSize.Value).Take(pageSize.Value);
                }
    
                return query.ToList();
            }
    
            public IQueryable<T> FindBy(Expression<Func<T, bool>> predicate)
            {
                return _context.Set<T>().Where(predicate);
            }
    
            public bool Exist(Expression<Func<T, bool>> predicate)
            {
                var exist = _context.Set<T>().Where(predicate);
                return exist.Any() ? true : false;
            }
        }

    Wednesday, August 26, 2020 1:39 PM
  • User475983607 posted

    I must be missing something but I don't understand how the code ever worked.  EF.CompileQuery<> takes at least two generic inputs not one.  Please see the docs for example code; https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/ef/language-reference/compiled-queries-linq-to-entities

    Wednesday, August 26, 2020 3:43 PM