I wish to apply ODataQueryOptions on to a DbSet. Let me give quick background of my app. I have a requirement to create an app that reads data from two tables T1 & T2. Both the tables have almost the same schema except a few columns in addition to each table. My entity class design is somewhat like below: (for the sake of brevity, I've kept the schema and nomenclature simple)
class Base
{
[Key]
public int ID;
public string Name;
public string Gender;
public string Comment;
}
[Table("T1")]
class TableOneEntity : Base
{
}
[Table("T2")]
class TableTwoEntity : Base
{
// extra columns from T2
public string Country;
public string City;
}
DbContext class:
class ApplicationDbContext : DbContext
{
public DbSet<TableOneEntity> TableOneDbSet;
public DbSet<TableTwoEntity> TableTwoDbSet;
}
My OData GET API looks like below:
public PageResult<TableTwoEntity> GetTableResult(ODataQueryOptions<TableOneEntity> options)
{
var result1 = options.ApplyTo(appDbCtx.TableOneDbSet) as IQueryable<TableOneEntity>;
var result2 = options.ApplyTo(appDbCtx.TableTwoDbSet) as IQueryable<TableTwoEntity>;
return new PageResult<TableTwoEntity>(output, odataProperties.NextLink, odataProperties.TotalCount);
}
When the GET API is called I get the following exception:
Cannot apply ODataQueryOptions of 'TableOneEntity' to IQueryable of 'TableTwoEntity'.
How can I apply the same 'options' to the TableTwoDbSet ?