Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EntityTypeConfiguration<TEntityType>.HasKey does not allow a nested expression #828

Open
AntonC9018 opened this issue Jan 28, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@AntonC9018
Copy link

AntonC9018 commented Jan 28, 2023

Assemblies affected

Microsoft.AspNetCore.OData 8.0.12

Describe the bug

EntityTypeConfiguration<TEntityType>.HasKey does not allow a nested expression.

I'm trying to set up odata with DTOs, mapping to EF Core queries with Automapper.
My DTO, which I'm trying to use as the OData entity, is set up differently than the EF Core entity, it has a nested type with the general information, which has the id on it.
However, HasKey only allows member expressions, so this won't work:

modelBuilder.EntityType<Dto>()
        .HasKey(a => a.General.Id);

Data Model

For the purposes of this issue, consider a reduced version:

public class Dto
{
    public DtoGeneral General { get; set; }
}
public class DtoGeneral
{
    public int Id { get; set; }
}

Additional Notes

Are nested things even allowed btw?
EF Core has owned types, are all types that aren't entities considered owned in OData?
My model has quite a bunch of nested types, It'd be unfortunate if I had to inline all that.

@AntonC9018 AntonC9018 added the bug Something isn't working label Jan 28, 2023
@julealgon
Copy link
Contributor

My model has quite a bunch of nested types, It'd be unfortunate if I had to inline all that.

Have you thought about just creating a computed redirection Id property on the parent that returns/ sets the value in the nested class?

I find it extremely unlikely that OData will ever support your scenario natively. But maybe I'm wrong.

@AntonC9018
Copy link
Author

My model has quite a bunch of nested types, It'd be unfortunate if I had to inline all that.

Have you thought about just creating a computed redirection Id property on the parent that returns/ sets the value in the nested class?

I find it extremely unlikely that OData will ever support your scenario natively. But maybe I'm wrong.

Yeah, that's quite an obvious workaround. What about my last question?

@julealgon
Copy link
Contributor

Yeah, that's quite an obvious workaround. What about my last question?

Types that are not entities in OData are considered complex types, not owned. Usually a type is not detected as an entity if it doesn't have an ID field.

There is the concept of an owned entity but that's just an entity that can only be accessed through another and doesn't have it's own entity set. These have to be configured as owned explicitly, either in the EDM, or through attributes.

@xuzhg
Copy link
Member

xuzhg commented Jan 30, 2023

Using complex type property to define key is valid in OData spec.

An example like this:

<EntityType Name="Category">
  <Key>
    <PropertyRef Name="Info/ID" Alias="EntityInfoID" />
  </Key>
  <Property Name="Info" Type="Sales.EntityInfo" Nullable="false" />
  <Property Name="Name" Type="Edm.String" />
</EntityType>

 

<ComplexType Name="EntityInfo">

  <Property Name="ID" Type="Edm.Int32" Nullable="false" />
  <Property Name="Created" Type="Edm.DateTimeOffset" />
</ComplexType>

But, it's not supported ODL. Once ODL supports it, I can make it work on the Web API side.

See issue at: OData/odata.net#1817

@habbes
Copy link
Contributor

habbes commented Jan 31, 2023

@ElizabethOkerio was once looking into adding key and property aliasing support in ODL.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants