Skip to content

Let me opt out from convention which treats Id property of an owned entity as a primary key. #35789

Closed as duplicate

Description

@voroninp

What problem are you trying to solve?

I am trying to map a collection of owned entities to json.

Owned entity has property Id and EF makes it an explicitly defined primary key, so I get the error:

'DbContext' of type ''. The exception 'Entity type 'FeatureId' is part of a collection mapped to JSON and has its ordinal key defined explicitly. Only implicitly defined ordinal keys are supported.' 

I could not find a method in Fluent Api to explicitly tell EF that Id should be treated as a regular property.

I'd like to avoid the need to rename Id property to something else.

Describe the solution you'd like

Well, there should be away to set an annotation for the property: b.Property(e => e.Id).NotKey().

Activity

  1. changed the title [-]Let me opt out from convention which treats Id property of an owned entity as primary key.[/-] [+]Let me opt out from convention which treats Id property of an owned entity as a primary key.[/+] on Mar 14, 2025
  2. AndriySvyryd commented on Mar 20, 2025

    @AndriySvyryd
    Member

    You should be able to work around this by creating a different property to be used as the key:

    modelBuilder.Entity<Customer>()
        .OwnsOne(customer => customer.Contact, b =>
        {
            b.ToJson();
            b.OwnsMany(e => e.Features, b =>
            {
                b.Property<int>("Key").HasJsonPropertyName("Id");
            });
        });

    The full fix is tracked in #28594

  3. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions