I am working on mapping OData QueryOptions to Automapper and then Entity Framework.
I am stuck with explicit expansion.
* If I do not define it, obviously all navigation properties do get epxanded. That DOES work.
then this does work - the navigation does not show up. This does work.
* If I add then property back then explicitly using ProjectTo<T>(expand); with expand being a string array [1] with [1] = "CustomerPortfolio", then I get an EF error:
innererror":{"message":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata.metadata=minimal'.","type":"System.InvalidOperationException","stacktrace":"","internalexception":{"message":"The specified type member 'Portfolio' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.","type":"System.NotSupportedException","stacktrace":" at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MemberAccessTranslator.TypedTranslate(ExpressionConverter parent, MemberExpression linq)\r\n at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)\r\n at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)\r\n at System.Data.Entity.Core.Objects.ELinq.ExpressionConverter.MemberInitTranslator.TypedTranslate(ExpressionConverter
(that is from OData, but as the stack trace shows this is EF internal).
* If I add the property back via the projected target name ("Portfolio") then I get the following error:
Object reference not set to an instance of an object.
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](IDictionary`2 parameters, IEnumerable`1 memberPathsToExpand)
at AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](Object parameters, Expression`1[] membersToExpand)
at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Object parameters, Expression`1[] membersToExpand)
at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, Object parameters, Expression`1[] membersToExpand)
at Red.Web.Api.Code.ODataExtensions.ProjectTo[S,T](IQueryable`1 query, ODataQueryOptions`1 options) in C:\Work\ObjectBrief\Red.Web.Api\Code\ODataExtensions.cs:line 31
at Red.Web.Api.Controllers.CodPropertyController.Get(ODataQueryOptions`1 options) in C:\Work\ObjectBrief\Red.Web.Api\Controllers\CodPropertyController.cs:line 29
at lambda_method(Closure , Object , Object[] )
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
Which is not really helpfull either. It seems that both ways are not working.
Anyone an idea how to get this working? Is this a known/documented bug?
The documentation at https://github.com/AutoMapper/AutoMapper/wiki/Queryable-Extensions
says:
To control which members are expanded during projection, set ExplicitExpansion in the configuration and then pass in the members you want to explicitly expand:
Which would indicate that the second version is correct - but that also generates a not too nice error.