I am using Microsoft's Odata v4 (Microsoft.ASPNet.OData 7.0.1) implementation under WebAPI (Microsoft.ASPNet.WebApi 5.2.4.net Framework) with EntityFramework 6.0 and am unable to determine if I am doing something wrong, or if the feature is unsupported by Microsoft's implementation. If not supported, how do I implement myself?
I have two entities with a one to one relationship. (Say Employee and EmployeeDetails).
http://localhost/odata/Employee?$expand=EmployeeDetails
works fine.
However http://localhost/odata/Employee?$expand=EmployeeDetails($select=Foo)
fails with "The query specified in the URI is not valid. The property 'Foo' cannot be used in the $select query option."
The entity classes is defined somewhat as follows:
public partial class Employee
{
[Key]
public int EmployeeID {get;set;}
[ForeignKey{"EmployeeDetail"}]
public int EmployeeDetailsID {get;set;}
}
public partial class EmployeeDetail
{
[Key]
public int EmployeeDetailsID {get;set;}
public string Foo {get;set;}
public string Fum {get;set;}
}
Controller is simply:
EFModel _db = new EFModel();
[EnableQuery(PageSize = 20,MaxExpansionDepth = 4,AllowedQueryOptions = AllowedQueryOptions.All)]
public IQueryable<Employee> Get()
{
return _db.Employees;
}
[EnableQuery(PageSize = 20,MaxExpansionDepth = 4,AllowedQueryOptions = AllowedQueryOptions.All)]
public IQueryable<Employee> GetEmployees()
{
return _db.Employees;
}
using the conventional modelBuilder I've allowed Filter, Expand on both entities.
Would prefer not to go the single view as my "employee" data is very wide and would like to have multiple one to one relationships.
Config Code
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapODataServiceRoute("odata", "odata", ModelBuilder.GetEdmModel())
}
}
Model Builder Class
public static class ModelBuilder
{
public static IEdmModel GetEdmModel()
{
var builder = new ODataConventionModelBuilder();
var EmployeeEntity = builder.EntitySet<Employee>("Employees");
EmployeeEntity.EntityType.Filter().Expand().Select().Count();
var EmployeeDetailEntity = builder.EntitySet<EmployeeDetail>("EmployeeDetails");
EmployeeDetailEntity.EntityType.Filter().Expand().Select().Count();
return builder.GetEdmModel();
}
}
http://localhost/odata/Employee?$select=EmployeeID
works? What is the version of OData WebApi and the code for configuration? – Pagottipublic EmployeeDetail EmployeeDetails { get; set; }
and fix foreign key attribute to[ForeignKey("EmployeeDetails")]
. I didn't figure out why your code doesn't work. – Pagotti