2

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.

8
  • 3
    What do you mean "override keyword doesn't work"? Commented Apr 12, 2013 at 13:47
  • 1
    I think you need the new keyword, not override. Commented Apr 12, 2013 at 13:50
  • If you are marking GetAll() as virtual in the base class and then overriding it in a child class with the override keyword, then the method overriding should work. What do you mean, it "doesn't work"? Details? Also can you give more info on your exception being thrown? Commented Apr 12, 2013 at 13:53
  • The code is executed from a Self Host Web Api so I get a general StackOverflowException error with both the new keyword and override keyword used in the ProductRepository If you notice, a virtual GetAll() is defined in the IRepository interface as well as in the EFRepository class. I think the code is barking not know which GetAll() to override? Commented Apr 12, 2013 at 14:06
  • well, with the way you have things setup I can guarantee that you need to be using the override keyword on the GetAll function in your child class. There is something else that is causing the stack overflow exception. check the rest of your code for infinite loops. Commented Apr 12, 2013 at 14:08

1 Answer 1

3

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(); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.