C# Hides Inherited Member

11,735

Mark ProductRepository.GetAll with the "new" keyword:

public new IQueryable<Product> GetAll()
{
    return DbSet.Include("Table1").Include("Table2").AsQueryable();
}

This will hide the method EFRepository.GetAll().

You can also choose to override the base method, if you want both methods to return the same result:

public override IQueryable<Product> GetAll()
{
    return DbSet.Include("Table1").Include("Table2").AsQueryable();
}
Share:
11,735
user2274587
Author by

user2274587

Updated on June 04, 2022

Comments

  • user2274587
    user2274587 almost 2 years

    I have this setup and get a compiler warning "... hides inherited member ...". How do I fix?

    public interface IRepository<T> where T : class
    {
        IQueryable<T> GetAll();
        T GetById(int id);
    }
    
    public class EFRepository<T> : IRepository<T> where T : class
    {
        public EFRepository(DbContext dbContext)
        {
            if (dbContext == null)
                throw new ArgumentNullException("dbContext");
            DbContext = dbContext;
            DbSet = DbContext.Set<T>();
        }
    
        protected DbContext DbContext { get; set; }
    
        protected DbSet<T> DbSet { get; set; }
    
        public virtual IQueryable<T> GetAll()
        {
            return DbSet;
        }
    
        public virtual T GetById(int id)
        {
            return DbSet.Find(id);
        }
    
    }
    
    public interface IProductRepository : IRepository<Product>
    {
        // Product specific interface code here
    }
    
    public class ProductRepository : EFRepository<Product>, IProductRepository
    {
        public ProductRepository(DbContext context) : base(context) { }
    
        public IQueryable<Product> GetAll()
        {
            return DbSet.Include("Table1").Include("Table2").AsQueryable();
        }
    }
    

    I get the compiler warning message but when running application, I get a StackOverflowException error. Adding the new keyword still generates the StackOverflowException error. Override keyword doesn't work. If I comment out the ProductRepository GetAll() method, all is fine and dandy. But I need to override the GetAll() method.

    Thanks.