0
public interface IDepartmentDataSource
{
    IQueryable<Department> Departments { get; }
}

public class DepartmentDb : DbContext, IDepartmentDataSource 
{
    //Error: property cannot implement property.... 
    public DbSet<Department> Departments { get; set; } 


    //should be: 
    //public IQueryable<Department> Departments { get; set; }
}

(Used code from Pluralsight)

From MSDN:

public class DbSet<TEntity> : DbQuery<TEntity>, 
IDbSet<TEntity>, IQueryable<TEntity>, IEnumerable<TEntity>, 
IQueryable, IEnumerable 
where TEntity : class

Why do I have to specifically implement as IQueryable ?

mishap
  • 8,176
  • 14
  • 61
  • 92

2 Answers2

1
public class DepartmentDb : DbContext, IDepartmentDataSource
{
    public DbSet<Department> Departments { get; set; }

    IQueryable<Department> IDepartmentDataSource.Departments
    {
        get { return Departments; }
    }
}

You should explicitly set your DbSet from the interface class IDepartmentDataSource.

Joseph Woodward
  • 9,191
  • 5
  • 44
  • 63
0
public class DepartmentDb : DbContext, IDepartmentDataSource 
{
    //Error: property cannot implement property.... 
    public DbSet<Department> Departments { get; set; } 
}

public class MyQueryable<T> : IQueryable<T> {}

....

MyDepartmentDb.Departments = new MyQueryable<Department>(); // Error!
// but it implements IDepartmentDataSource
// which should let any IQueryable<Department> in, so what gives??

Because the code above wouldn't work, but should. i.e. you should be able to assign any IQueryable<Department> to the Departments property, not just a DbSet<Department>.

George Duckett
  • 31,770
  • 9
  • 95
  • 162