OData V4 .NET Core AutoMapper nested properties cannot be translated from Linq

42 views
Skip to first unread message

pma...@gmail.com

unread,
May 7, 2020, 9:43:14 AM5/7/20
to AutoMapper-users

I am trying to get the OData Query "orderby" on a nested property to work on .Net Core 3.1

According to my reading the correct query should be formatted like this:

https://{server}:{port}/v1/Entities?$expand=ownBy&$orderby=ownBy/userName

and I get the following error message:

System.InvalidOperationException: The LINQ expression 'DbSet<Entity>
    .LeftJoin(
          outer: DbSet<User>,
        inner: p => EF.Property<Nullable<Guid>>(p, "OwnById"), outerKeySelector: u => EF.Property<Nullable<Guid>>(u, "UserId"),
        innerKeySelector: (o, i) => new TransparentIdentifier<Portfolio, User>(
            Outer = o,
            Inner = i
        ))
    .OrderBy(p => EF.Property<Nullable<Guid>>(p.Inner, "UserId") == null ? null : new UserModel{
        UserEmail = p.Inner.UserEmail,
        UserFirstName = p.Inner.UserFirstName,
        UserId = p.Inner.UserId,
        UserLastName = p.Inner.UserLastName,
        Username = p.Inner.Username
    }
    .Username)'
could not be translated.

It seems to be a fairly simple example, but the linq query seems to be mixing BbSet entity with DtoEntity which may come from AutoMapper.

Here is my controller using AutoMapper:

 [ODataRoute]
   
[Produces("application/json")]
   
[ProducesResponseType(typeof(ODataValue<IEnumerable<EntityModel>>), Status200OK)]
   
[ProducesResponseType(Status404NotFound)]
   
[EnableQuery(AllowedQueryOptions = All, MaxTop = 100)]
   
public IQueryable<EntityModel> Get()
   
{
       
var results = _Query.Get();
       
return results.ProjectTo<EntityModel>(_Mapper.ConfigurationProvider);
   
}

AutoMapper config:

CreateMap<Entity, EntityModel>();
Metadata:

<EntityType Name="EntityModel">
       
<Key>
           
<PropertyRef Name="entityId" />
       
</Key>
        ...
       
<Property Name="ownById" Type="Edm.Guid" />
        ...
       
<NavigationProperty Name="ownBy" Type="AMI.UserModel" />
   
</EntityType>

   
<EntityType Name="UserModel">
       
<Key>
           
<PropertyRef Name="userId" />
       
</Key>
       
<Property Name="userId" Type="Edm.Guid" Nullable="false" />
       
<Property Name="username" Type="Edm.String" />
       
<Property Name="userFirstName" Type="Edm.String" />
       
<Property Name="userLastName" Type="Edm.String" />
       
<Property Name="userEmail" Type="Edm.String" />
   
</EntityType>

Any hint would be very much appreciated




Jimmy Bogard

unread,
May 7, 2020, 11:45:57 AM5/7/20
to automapp...@googlegroups.com

There’s a NuGet package explicitly for OData and expression mapping, have you looked at that?

 

https://docs.automapper.org/en/stable/Expression-Translation-(UseAsDataSource).html

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/automapper-users/96ea9942-23c6-42bf-8065-47e057618785%40googlegroups.com.

 

Reply all
Reply to author
Forward
0 new messages