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; }
}

(使用来自 Pluralsight 的代码)

来自 MSDN:

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

为什么我必须专门实现为 IQueryable ?

4

2 回答 2

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

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

您应该DbSet从接口类显式设置您的IDepartmentDataSource

于 2014-12-24T09:00:57.410 回答
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??

因为上面的代码不起作用,但应该。即,您应该能够将 any 分配IQueryable<Department>给该Departments属性,而不仅仅是DbSet<Department>.

于 2013-05-01T14:32:42.627 回答