Y
Y
Ytsu Ytsuevich2016-01-16 14:06:08
ASP.NET
Ytsu Ytsuevich, 2016-01-16 14:06:08

How to use the Include and AsNoTracking methods using the Repository pattern?

My current repository looks something like this:

class UserRepository : IRepository<User>  // User - сущность entity
{
  // контекст подключения к базе данных. Инициализируется в конструкторе
  private readonly EntityContext _ctx;

  public IQueryable<User> GetAll(){
    return _ctx.User;
  }
  // ещё методы Save (INSERT), Update, Delete,
  // но сейчас разбираем только выборку
  // ...
}

So, the GetAll() method returns IQueryable and not DbSet , which means that it will not be possible to call the wonderful Include, AsNoTracking , etc. methods.
To declare the return type of DbSet in the GetAll() method (in the IRepository interface ) means to bind to the Entity framework, which I (for some reason) do not want. Maybe you need to?
What do they do in this situation?
(IMHO, adding these methods to every repository is an even worse thing)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Mozhaykin, 2016-01-16
@smozhaykin

I recently started learning about the structure of nopcommerce . There it is implemented as follows :

/// <summary>
        /// Gets a table
        /// </summary>
        public virtual IQueryable<T> Table
        {
            get
            {
                return this.Entities;
            }
        }

        /// <summary>
        /// Gets a table with "no tracking" enabled (EF feature) Use it only when you load record(s) only for read-only operations
        /// </summary>
        public virtual IQueryable<T> TableNoTracking
        {
            get
            {
                return this.Entities.AsNoTracking();
            }
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question