This project is read-only.


$expand fails when the navigation property being expanded is null.




    public abstract class EntityNome : IEntity
        public int Id { get; set; }
        public string Nome { get; set; }
    public class Passo : EntityNome, IAuditavel
        public virtual Passo ProximoPasso { get; set; }
        public virtual ICollection<Usuario> Responsaveis { get; set; }
        public virtual ICollection<CheckListItemTemplate> CheckListItens { get; set; }
    "odata.error": {
        "code": "",
        "message": {
            "lang": "en-US",
            "value": "An error has occurred."
        "innererror": {
            "message": "Exception has been thrown by the target of an invocation.",
            "type": "System.Reflection.TargetInvocationException",
            "stacktrace": "   at System.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__a.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()",
            "internalexception": {
                "message": "The cast to value type 'Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.",
                "type": "System.InvalidOperationException",
                "stacktrace": "   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal)\r\n   at lambda_method(Closure , Shaper )\r\n   at System.Data.Entity.Core.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper)\r\n   at System.Data.Entity.Core.Common.Internal.Materialization.Shaper`1.SimpleEnumerator.MoveNext()\r\n   at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()\r\n   at System.Web.Http.OData.Query.ODataQueryOptions.LimitResults[T](IQueryable`1 queryable, Int32 limit, Boolean& resultsLimited)"
Closed Jul 19, 2013 at 12:03 AM by jacalvar


raghuramn wrote Jun 13, 2013 at 6:57 PM

Investigated this further. The root cause here is that the expanded navigation property is null. There are two issues here,

1) If the expanded navigation property is null, we still get it as SelectExpandWrapper. This is because entityframework cannot create nulls of non-entity types.
2) If there is a projection on the expanded navigation property, i.e lets say $select=Customer/ID&$expand=Customer and Customer is null, entityframework projects Customer/ID as null and tries to assign it to an int (as ID is an int). It fails in the materializer.