2
votes

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();
    }
}
1
http://localhost/odata/Employee?$select=EmployeeID works? What is the version of OData WebApi and the code for configuration?Pagotti
@Pagotti. Updated example. Included GetEmployees which handles your example to the code.Spevy
I replicate your code and works for me. The only difference was I change the model to add public EmployeeDetail EmployeeDetails { get; set; } and fix foreign key attribute to [ForeignKey("EmployeeDetails")]. I didn't figure out why your code doesn't work.Pagotti
@Pagotti. Thanks, I rebuilt and it seems to be working. I must have had a typo or something in my original code which didn't translate into the sample code I put together. You did help me out by confirming it should work.Spevy

1 Answers

0
votes

As noted the above sample code code works with minor corrections and when put back into a sample project. I've since rebuilt my original project and now believe the issue was an error/typo in my original code.