4

I'm having a strange issue with $select in WCF Data Services combined with Entity Framework. The Entity Framework entities are not being directly exposed by the service. Rather, EF is being queried, and the results are being projected onto a DTO that IS being exposed.

Here is an example of queries that are working great, all the way down to properly formed queries in the database:

/Test.svc/Files

/Test.svc/Files?$filter=Name eq 'sample'

But this will NOT work:

/Test.svc/Files?$select=Id

It throws this exception within the EntityFramework code base:

The argument to DbIsNullExpression must refer to a primitive, enumeration or reference type.

...at this point in the call stack:

System.Data.Entity.Core.Common.CommandTrees.ExpressionBuilder.DbExpressionBuilder.ValidateIsNull(DbExpression argument, Boolean allowRowType)

See below for details the entities in question. The IQueryable property is on the data source container (implementing IUpdatable), which is being used by the service.

  • File is the entity framework object being pulled by dbContext.Files
  • EdmFile is the DTO that I am projecting to, and exposing via the OData service.

Here is the IQueryable exposed by my data container:

public IQueryable<EdmFile> Files
{
    get
    {

        var files = dbContext.Files //EF collection of File
            .Select(f => new EdmFile() { Id = f.Id, Name = f.Name });

        return files; //return projection onto EdmFile
    }
}

If I return files.ToList(), the $select will work. However, the IQueryable will then be prematurely enumerated and ALL of the files (ignoring any requested $filter, etc.) will be retrieved from the database.

Here is the entity/DTO I am actually exposing:

[DataServiceKey("Id")]
public class EdmFile
{
    public long Id { get; set; }
    public string Name { get; set; }
}

Here is the Entity Framework entity:

public partial class File
{
    public long Id { get; set; }
    public string Name { get; set; }
}

I have tried repeatedly Googling this and perusing SO, but am not able to get anywhere with the results. Any help would be appreciated on this!!

Josh
  • 805
  • 10
  • 22

1 Answers1

0

I don't know what version of EntityFramework you are usinge, but it looks like this issue was fixed in EntityFramework 6.1.0

Here is the issue on codeplex: https://entityframework.codeplex.com/workitem/826

Here is the SO question and answer that helped me: https://stackoverflow.com/a/25320462/466100

Community
  • 1
  • 1
Gene C
  • 2,010
  • 26
  • 34