mishap
mishap

Reputation: 8505

DbSet<TEntity>, IQueryable<TEntity> - OOP concepts

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 ?

Upvotes: 0

Views: 615

Answers (2)

Hamza Yahia
Hamza Yahia

Reputation: 41

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.

Upvotes: 1

George Duckett
George Duckett

Reputation: 32418

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>.

Upvotes: 0

Related Questions